-2

I have a multidimensional array in PHP and I want to un-nest all elements to bring them to the top level. Here is the array:

Array
(
    [0] => Array
        (
            [id] => 1
            [color] => red

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [color] => blue
                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [color] => green
                        )

                )

            [2] => Array
                (
                    [id] => 4
                    [color] => blue
                )

        )

    [2] => Array
        (
            [id] => 5
            [color] => purple
        )

)

What I want end the end is this... so basically it loops over the array and any group it just unnests it. Sorry, I don't know the proper terminology. Is there PHP function for this? I've tried several solutions on stack but it moves all key values pairs up to the top and not just the array groups --- it just creates a mess.

(
    [0] => Array
        (
            [id] => 1
            [id] => red                
        )
    [1] => Array
        (
            [id] => 2
            [color] => blue
        )

    [2] => Array
        (
            [id] => 3
            [color] => green
        )

    [3] => Array
        (
            [id] => 4
            [color] => blue
        )


    [4] => Array
        (
            [id] => 5
            [color] => purple
        )

)

Thanks!

LargeTuna
  • 2,694
  • 7
  • 46
  • 92

3 Answers3

0

You can do it like this, check the Demo

$result = [];
array_walk_recursive($array,function(&$v)use($result){
   $result[] = $v;
});
$result = array_chunk($result,2);
$result = array_map(function($v){return array("id"=>$v[0],"color"=>$v[1]);});
print_r($result);
LF00
  • 27,015
  • 29
  • 156
  • 295
0

You can unpack it by using array_walk_recursive

$i = 0;
array_walk_recursive($a, function($v,$k)use(&$r,&$i){
  $r[$i][$k] = $v;
  ($k == 'color') ? $i++ :$i; 
});
print_r($r);

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

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

Use simple generic recursion as below(no id, no color),

function flattenArray($arr = [])
{
    $retArr = [];
    foreach ($arr as $val) {
        if (is_array($val)) {
            $retArr[] = array_filter($val, function ($v) {
                return !is_array($v);
            });
            $retArr = array_merge(array_filter($retArr), flattenArray($val));
        }
    }
    return $retArr;
}
$arr = flattenArray($arr);

Demo

Output:-

Array
(
    [0] => Array
        (
            [id] => 1
            [color] => red
        )

    [1] => Array
        (
            [id] => 2
            [color] => blue
        )

    [2] => Array
        (
            [id] => 3
            [color] => green
        )

    [3] => Array
        (
            [id] => 4
            [color] => blue
        )

    [4] => Array
        (
            [id] => 5
            [color] => purple
        )

)
Rahul
  • 18,271
  • 7
  • 41
  • 60