-1

I have an array with 3 levels and I'd like to merge/flatten all 3rd level subarrays into one subarray/row on the 2nd level.

$array = [
    [
        'User' => [
            'id' => 57341,
            'updated' => null,
            'userId' => 57341,
            'value' => null,
            'lat' => 53.4537812,
            'lon' => -2.1792437,
        ],
        [
            'feed_likes' => 'NA',
            'category_idx' => -1,
            'type' => 'User'
        ]
    ],
    [
        'User' => [
            'id' => 57336,
            'updated' => null,
            'userId' => 57336,
            'value' => null,
            'lat' => 53.473684,
            'lon' => -2.2399827,
        ],
        [
            'feed_likes' => 'NA',
            'category_idx' => -1,
            'type' => 'User'
        ]
    ],
];

The deep User-keyed subarrays (having 6 elements) should be merged with its sibling/indexed subarray (having 3 elements) to form a 9-element row on the second level.

Desired result:

[
    [
        'id' => 57341,
        'updated' => null,
        'userId' => 57341,
        'value' => null,
        'lat' => 53.4537812,
        'lon' => -2.1792437,
        'feed_likes' => 'NA',
        'category_idx' => -1,
        'type' => 'User'
    ],
    [
        'id' => 57336,
        'updated' => null,
        'userId' => 57336,
        'value' => null,
        'lat' => 53.473684,
        'lon' => -2.2399827,
        'feed_likes' => 'NA',
        'category_idx' => -1,
        'type' => 'User'
    ]
]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

4 Answers4

1

You can use splat ... operator with array_merge

foreach($a as $child){
  $flatten[] = array_merge(...array_values($child));
}

Working example :- https://3v4l.org/HkUh6

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

Here's a reduced version of your array, but you can merge your inner arrays.

$data =
[
    [
        'User' => [
            'id' => 57341
        ],
        [
            'type' => 'User'
        ]
    ],
    [
        'User' => [
            'id' => 57336,
        ],
        [
            'type' => 'User'
        ]
    ]
];

$result =
array_map(
    function($v) {
        return array_merge($v['User'], $v[0]);
    },
    $data
);

var_export($result);

Output:

array (
  0 => 
  array (
    'id' => 57341,
    'type' => 'User'
  ),
  1 => 
  array (
    'id' => 57336,
    'type' => 'User'
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Progrock
  • 7,373
  • 1
  • 19
  • 25
0

To merge and flatten the 3rd level data sets into consolidated 2nd level rows with functional style programming, make iterated calls of array_merge() which receive all 3rd level payloads at once. The spread operator (...) is a concise technique used to unpack multiple elements in an array. A special consideration is needed for this case because spreading elements which have non-numeric keys will cause code breakage. To overcome this, simply call array_values() to "index" the array (replace all keys with sequenial numbers) before spreading.

Code: (Demo)

var_export(
    array_map(
        fn($rows) => array_merge(...array_values($rows)),
        $array
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
-1

If I understood you correctly you need to merge User array and in this case with the second array in that key. it that case something like this should work

foreach($array as $key=>$deep1){
    $newArray = [];
    foreach($deep1 as $deep2){
       $newArray = array_merge($newArray,$deep2)
    }
    $array[$key] = $newArray;
}

Did II understand your question correctly?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
LightNight
  • 851
  • 1
  • 6
  • 14
  • Even after adding the missing semicolon, this answer does not produce the desired result. https://3v4l.org/bgGbF – mickmackusa Oct 01 '22 at 23:49