0

In the middle of declaring an array of arrays, I want to "write" an array of arrays generated by my function.

I have a working example when I:

  1. simply store my function-generated arrays into a variable and
  2. then call each array from that function by its key,

but I can't find a command to simply call everything at once.

Here is the code which (I hope) explains it:

<?php

// A. (THIS WORKS)

// A1: A function that returns an array of arrays
function my_arrays_building_function() {
    $first_array = array(
        'id'        => 'my_array_1',
        'type'      => 'some-type',
        'title'     => 'My title 1',
    );
    $second_array = array(
        'id'        => 'my_array_2',
        'type'      => 'some-type',
        'title'     => 'My title 2',
    );
    // ... and so on, many more.
    return array(
        'first-array' => $first_array,
        'second-array' => $second_array,
        // ... and so on.
    );
    // NOTE there are tens or hundreds of returned arrays here.
}


// A2: Store my arrays in a variable
$my_array = my_arrays_building_function();


// A3: Inside an array (of arrays), I simply "write" my arrays INDIVIDUALLY and THAT works
array(

    array(
        'id'        => 'dummy_preexisting_array_1',
        'type'      => 'some-type',
    ),

    array(
        'id'        => 'dummy_preexisting_array_2',
        'type'      => 'some-type',
    ),

    // HERE THERY ARE, INDIVIDUALLY, COMMA SEPARATED
    $my_array[ 'first-array' ],
    $my_array[ 'second-array' ],

    array(
        'id'        => 'dummy_preexisting_array_n',
        'type'      => 'some-type',
    )

),

/** -------------------- //
        THE ISSUE
// -------------------- **/

// B: HOW DO I "write" THEM ALL AT ONCE???

// B1: The same as A1
function my_arrays_building_function() {
    $first_array = array(
        'id'        => 'my_array_1',
        'type'      => 'some-type',
        'title'     => 'My title 1',
    );
    $second_array = array(
        'id'        => 'my_array_2',
        'type'      => 'some-type',
        'title'     => 'My title 2',
    );

    // NOT SURE I SHOULD RETURN LIKE THIS
    return array(
        'first-array' => $first_array,
        'second-array' => $second_array
    );
}

// B2: Same as A3, Inside an array (of arrays), I "write" my arrays BUT NOW I WANT TO "WRITE" THEM ALL AT ONCE
array(

    array(
        'id'        => 'dummy_preexisting_array_1',
        'type'      => 'some-type',
    ),

    array(
        'id'        => 'dummy_preexisting_array_2',
        'type'      => 'some-type',
    ),

    /** >>>> I need my arrays here ALL AT ONCE aka NOT INDIVIDUALLY AS IN EXAMPLE A. <<<< **/
    /** 
     * In other words, while I'm declaring this array, 
     * I simply need all my arrays from my_arrays_building_function() 
     * "written" here with a simple command instead of calling hundreds
     * of arrays individually as in the first example
     */

    array(
        'id'        => 'dummy_preexisting_array_n',
        'type'      => 'some-type',
    )

), /* this goes on as it's a part of even bigger array */
1

3 Answers 3

0
  • Although I wouldn't recommend declaring hundreds of array variables inside a function because that's crazy, but for now, you can use get_defined_vars() to get over this issue.

  • You will also need to filter out the variables which are arrays and has the keys id, type and title as there are could be several other variables defined apart from this.

Snippet:

<?php

array_filter(get_defined_vars(), fn($val) => is_array($val) && isset($val['id'], $val['type'], $val['title']));

Online Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but I don't think this'll do because my arrays are all different and not all contain and only contain 'id', 'type' and 'title'.
@oneone Bad practice to declare hundreds of array variables to begin with. You will need to show the possible keys for those arrays then.
Thank you @nice_dev but Rylee suggested Array Unpacking and it worked. Thank you again for your suggestion.
@oneone I am still confused what you wanted in the first place but glad you solved it.
0

Not too sure if I'm understanding this correctly but from what I assume, you just want to return an array with a bunch of others inside it that you define throughout the function?

A simple approach for this would be to define your output variable immediately and add all of your other arrays to it:


function my_arrays_building_function() {
    $output = [];

    $output[] = [
        'id'        => 'my_array_1',
        'type'      => 'some-type',
        'title'     => 'My title 1',
    ];

    $output[] = [
        'id'        => 'my_array_2',
        'type'      => 'some-type',
        'title'     => 'My title 2',
    ];

    return $output;
}

3 Comments

OK, but nothing happens when I call it in the "I need my arrays here" part. If I say: $myvar = my_arrays_building_function(); and then while declaring the master array I do this: $myvar, Nothing happens, if I echo (or print_r) the code ends up in the frontend and not as the actual part of the master array.
@oneone I don't think your question is clear enough. If you want to merge arrays, you can use array_merge which combines elements from multiple arrays. If you want to insert an array inside another existing one, use array_splice. You can also look at Array Unpacking for PHP7.4+. If you need to define an array with nested arrays, simply include them as array elements.
Array Unpacking, That's it! The three dots of doom ffs. Thank you! The unpacking worked but the prep is a bit different from the one in your comment so I'll have to post the solution comment. But how can I upvote you or send thanks or something like that?
0

Thank you to @Rylee for suggesting Array Unpacking.

The final code would look like this:

// C1: A function that returns an array of arrays BUT without keys
function my_arrays_building_function() {
    $first_array = array(
        'id'        => 'my_array_1',
        'type'      => 'some-type',
        'title'     => 'My title 1',
    );
    $second_array = array(
        'id'        => 'my_array_2',
        'type'      => 'some-type',
        'title'     => 'My title 2',
    );
    // ... and so on, many more.

    // Then return but now I don't assign keys, I only list the vars.
    return array(
        $first_array, $second_array, ... and so on.
    );
}

// C2: Inside an array (of arrays), I use Array Unpacking, THAT'S WHAT I WAS LOOKING FOR, UNPACK ARRAY! SEE BELOW
array(

    array(
        'id'        => 'dummy_preexisting_array_1',
        'type'      => 'some-type',
    ),

    array(
        'id'        => 'dummy_preexisting_array_2',
        'type'      => 'some-type',
    ),

    // HERE I UNPACK MY ARRAY BY USING ... THE THREE DOTS ARE THE KEY
    ... my_arrays_building_function(),

    array(
        'id'        => 'dummy_preexisting_array_n',
        'type'      => 'some-type',
    )

),

Hooray!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.