skip to Main Content

I want to combine these arrays in a new array based on the same key number for creating tables. This is my code.

$arr3 = array();

print '<pre>';
print_r($bonus_table);
print '</pre>';

foreach($bonus_table as $key => $row) {
    $arr3 = array_merge($row['cells'],$arr3);
}

return $arr3;

And this is the output from my code. Basically this is the current structure of array:

Array
  (
            [0] => Array
                (
                    [cells] => Array
                        (
                            [0] => DEPOTS
                            [1] => PRICES
                        )

                )

            [1] => Array
                (
                    [cells] => Array
                        (
                            [0] => 50
                            [1] => 25
                        )

                )

            [2] => Array
                (
                    [cells] => Array
                        (
                            [0] => 100
                            [1] => 50
                        )

                )
)

But I want to convert the above output with the below structure. The desired array should look like this:

Array
        (
            [0] => Array
                (
                    [cells] => Array
                        (
                            [0] => DEPOTS
                            [1] => 50
                            [2] => 100
                        )

                )
            [1] => Array
                (
                    [cells] => Array
                        (
                            [0] => PRICES
                            [1] => 25
                            [2] => 50
                        )
                )
         )

3

Answers


  1. Replacing your foreach loop with the below will produce the data you are after:

    $arr3 = [
        [
            'cells' => ['Depots']
        ],
        [
            'cells' => ['Prices']
        ]
    ];
    
    foreach ($bonus_table as $input) {
        if (is_numeric($input['cells'][0]) && is_numeric($input['cells'][1])) {
            $output_array[0]['cells'][] = $input['cells'][0];
            $output_array[1]['cells'][] = $input['cells'][1];
        }
    }
    

    Demo: https://onlinephp.io/c/06b36

    Here we are looping through the input array ($bonus_table) and collecting data in an output array ($arr3).

    Login or Signup to reply.
  2. You simply could use array_column

    <?php
    
    $bonus_table = [];
    $bonus_table[]['cells'] = ["DEPOTS", "PRICES"];
    $bonus_table[]['cells'] = [50, 25];
    $bonus_table[]['cells'] = [100, 50];
    
    $formattedArray = [];
    
    // The bonus_table first level could be useless
    $cellsArray = array_column($bonus_table, 'cells');
    
    foreach($cellsArray as $key => $row) {
        if (array_key_exists($key, $row)) {
            $formattedArray[]["cells"] = array_column($cellsArr, $key);
        }
    }
    

    outout

    Array
    (
        [0] => Array
            (
                [cells] => Array
                    (
                        [0] => DEPOTS
                        [1] => 50
                        [2] => 100
                    )
    
            )
    
        [1] => Array
            (
                [cells] => Array
                    (
                        [0] => PRICES
                        [1] => 25
                        [2] => 50
                    )
    
            )
    
    )
    
    Login or Signup to reply.
  3. Because you have 3 levels in your array and the middle level uses cells as the key which needs to be retained, array transposition can be done swiftly without out any function calls by nesting a loop inside of another.

    For the record, I don’t see a lot of value in having the redundant cells level in your array structure.

    Code: (Demo)

    $result = [];
    foreach ($array as ['cells' => $data]) {
        foreach ($data as $i => $v) {
            $result[$i]['cells'][] = $v;
        }
    }
    var_export($result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search