Maneesh MK
Maneesh MK

Reputation: 395

Flatten a multdimensional Tree array in PHP

I have a Tree structured array with parent child relations. I want the array to be flattened for easy parsing.

Array
(
[0] => Array
    (
        [ParentID] => 11653
        [FolderID] => 11823
        [Items] => Array
            (
                [0] => Array
                    (
                        [ParentID] => 11823
                        [FolderID] => 11824
                        [Items] => Array
                            (
                                [0] => Array
                                    (
                                        [ParentID] => 11824
                                        [FolderID] => 11827
                                        [Items] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [ParentID] => 11827
                                                        [FolderID] => 11828
                                                    )

                                            )

                                    )

                            )

                    )

                [1] => Array
                    (
                        [ParentID] => 11823
                        [FolderID] => 11825
                        [Items] => Array
                            (
                                [0] => Array
                                    (
                                        [ParentID] => 11825
                                        [FolderID] => 11826
                                    )

                            )

                    )

            )

    )

)

Desired output :

Array
(
[0] => Array
    (
        [ParentID] => 11653
        [FolderID] => 11823
 )
[1] => Array
      (
    [ParentID] => 11823
    [FolderID] => 11824
   )
[2] => Array
        (
    [ParentID] => 11824
    [FolderID] => 11827
    )
[3] => Array
        (
    [ParentID] => 11827
    [FolderID] => 11828
    )
[4] => Array
        (
    [ParentID] => 11823
    [FolderID] => 11825
    )
[5] => Array
(
    [ParentID] => 11825
    [FolderID] => 11826
)

)

I Have tried many flattening logic found in here, but i couldn't get the desired output.

Upvotes: 1

Views: 1015

Answers (3)

Teaqu
Teaqu

Reputation: 3263

Using array_walk_recursive where $array is your array and $new_array is the flattened array.

array_walk_recursive($array, function($item, $key) use (&$new_array, &$i)
{
    $new_array[(int) $i][$key] = $item; 
    ($key == 'ParentID') ?: $i++;
});

var_dump($new_array);

If you want it for parsing you could also just do:

array_walk_recursive($array, function($item, $key)
{
    *Your parsing for each array*
});

Upvotes: 2

James Craig
James Craig

Reputation: 6854

This function works well for flattening multi-dimensional arrays:

function array_flatten($array)
{ 
    if(!is_array($array)) { 
        return FALSE; 
    } 

    $result = array(); 

    foreach($array as $key => $value) { 
        if(is_array($value)) { 
            $result = array_merge($result, array_flatten($value)); 
        } else { 
          $result[$key] = $value; 
        } 
    }

    return $result; 
}

Upvotes: 1

user1032531
user1032531

Reputation: 26281

Start with a static variable.

Then create a function which accepts an array, iterates over it, and adds an element to your static variable with the ParentID and FolderID. If the array also contains an element with name "Items" and is an array, then apply it recursively to this function.

Upvotes: -1

Related Questions