0

I want to break the below nested array in simple associative array.

Input

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Gadgets
            [code] => gadget
            [parent_id] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => Mobile
                            [code] => mobile
                            [parent_id] => 1
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [name] => Laptops
                            [code] => laptop
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [name] => Dell
                                            [code] => dell
                                            [parent_id] => 3
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 5
                                            [name] => Lenovo
                                            [code] => lenovo
                                            [parent_id] => 3
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

Output

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Gadgets
            [code] => gadget
            [parent_id] => 
        )

    [1] => Array
        (
            [id] => 2
            [name] => Mobile
            [code] => mobile
            [parent_id] => 1
        )

    [2] => Array
        (
            [id] => 3
            [name] => Laptops
            [code] => laptop
            [parent_id] => 1
        )

    [3] => Array
        (
            [id] => 4
            [name] => Dell
            [code] => dell
            [parent_id] => 3
        )

    [4] => Array
        (
            [id] => 5
            [name] => Lenovo
            [code] => lenovo
            [parent_id] => 3
        )
)

Need help in making this type of array from the given array. I tried many things with for loops, but get stuck when in case there are many nested array and that solution does not fit correctly to my requirement.

There is one root node and others are child nodes and many parent nodes can have child nodes.

flixy
  • 73
  • 2
  • 10
  • Does this answer your question? [How to Flatten a Multidimensional Array?](https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – El_Vanja Apr 29 '21 at 19:39

2 Answers2

1

There are a ton of ways to do this, here are a couple of simple examples. If you don;t care about maintaining order, the recursive function is pretty simple. If you do need to maintain the order of the elements as they are encountered while traversing the tree (to render them as tables for example), it's just a bit more of a faff.

<?php
function flattenTree($array)
{
    $output = [];
    foreach($array as $currBranch)
    {
        if(!empty($currBranch['children']))
        {
            $children = flattenTree($currBranch['children']);

            $output = array_merge($output, $children);
        }

        unset($currBranch['children']);

        $output[] = $currBranch;
    }

    return $output;
}

function flattenTreeMaintainingOrder($array)
{
    $output = [];
    foreach($array as $currBranch)
    {
        $children = (array_key_exists('children', $currBranch)) ? $currBranch['children']:[];

        unset($currBranch['children']);

        $output[] = $currBranch;

        if(!empty($children))
        {
            $children = flattenTreeMaintainingOrder($children);

            $output = array_merge($output, $children);
        }
    }

    return $output;
}

$flat = flattenTree($array);
$flatOrdered = flattenTreeMaintainingOrder($array);

print_r($flat) . PHP_EOL;
print_r($flatOrdered) . PHP_EOL;
Rob Ruchte
  • 3,569
  • 1
  • 16
  • 18
0

A recursive function is one option...

function extractChildren($parent, $farr) {
 $children = $parent['children'];
 if (!$children || count($children)==0) return $farr;
 unset($parent['children']);
 $farr[]= $parent;
 return extractChildren($children, $farr);
}

$finalarray=array(); 

// $array is the array you have in your question 
foreach ($array as $parent) {
 $finalarray = extractChildren($parent, $finalarray);
}

As @El_Vanya mentioned above, there are scads of other ways to accomplish this here: How to Flatten a Multidimensional Array?

Kinglish
  • 23,358
  • 3
  • 22
  • 43