Home > Blockchain >  PHP Write "an array of arrays" dynamically inside an array of arrays, while declaring it
PHP Write "an array of arrays" dynamically inside an array of arrays, while declaring it

Time:09-26

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 */

CodePudding user response:

  • 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

CodePudding user response:

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;
}
  • Related