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.

1

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.