2

I have an array which comes out by calling a recursion function on the basis of parent id. This array is an n level multidimensional array. What I want is to break this array into single dimensional, so that every child comes just after their parent. I am using following function to first convert into recursive tree.

function formatTree($tree, $parent){
        $tree2 = array();
        foreach($tree as $i => $item){
            if($item['cat_parent_id'] == $parent){
                $tree2[$item['cat_id']] = $item;
                $tree2[$item['cat_id']]['submenu'] = formatTree($tree, $item['cat_id']);
            } 
        }

        return $tree2;
    }

This is the array I have.

Array
(
    [58] => Array
        (
            [cat_id] => 58
            [cat_name] => Desserts
            [cat_parent_id] => 0
            [submenu] => Array
                (
                    [535] => Array
                        (
                            [cat_id] => 535
                            [cat_name] => dessert child
                            [cat_parent_id] => 58
                            [submenu] => Array
                                (
                                )

                        )

                )

        )

    [56] => Array
        (
            [cat_id] => 56
            [cat_name] => Biryani & Rice
            [cat_parent_id] => 0
            [submenu] => Array
                (
                )

        )
)

This is how I want this.

Array
(
    [0] => Array
        (
            [cat_id] => 58
            [cat_name] => Desserts
            [cat_parent_id] => 0
            [submenu] => Array
                (

                )

        )
    [1] => Array
                    (
                        [cat_id] => 535
                        [cat_name] => dessert child
                        [cat_parent_id] => 58
                        [submenu] => Array
                            (
                            )

                    )    

    [2] => Array
        (
            [cat_id] => 56
            [cat_name] => Biryani & Rice
            [cat_parent_id] => 0
            [submenu] => Array
                (
                )

        )
)
1
  • There's no one-size-fits-all answer to this in general. In your specific case you could use a recursive function that calls itself on the submenu of a given node if it's not empty. Commented Aug 18, 2015 at 12:24

5 Answers 5

1

It's almost 2020. I know it's a bit late but for those who are googling around: Their answers were close but they're only returning the parent elements. I've tweaked it a little bit to make it work (using array_merge).

function array_single_dimensional($items)
{
    $singleDimensional = [];
    foreach ($items as $item) {
        $children =  isset($item['children']) ? $item['children'] : null; //temporarily store children if set
        unset($item['children']); //delete children before adding to new array
        $singleDimensional[] = $item; // add parent to new array
        if ( !empty($children) ){ // if has children
            //convert children to single dimensional
            $childrenSingleDimensional = array_single_dimensional($children);

            //merge the two, this line did the trick!
            $singleDimensional = array_merge($singleDimensional, $childrenSingleDimensional); 
        }
    }
    return $singleDimensional;
}

$singleDimensionalArray = array_single_dimensional($multidimensionalArray);
Sign up to request clarification or add additional context in comments.

1 Comment

yes its working fine for me. More than 2 level its also working. Thanks
0

So, I assume you can change your initial function to make an array like the second one... then you should update your function like this:

function formatTree($tree, $parent){
        $tree2 = array();
        foreach($tree as $i => $item){
            if($item['cat_parent_id'] == $parent){
                $item['submenu'] = array();
                $tree2[] = $item;
                formatTree($tree, $item['cat_id']);
            } 
        }
        return $tree2;
    }

1 Comment

This will break sub array into single array , but I need that to be just after their parent.
0

This should work. For your use case you don't need a parent_id in your function.

function formatTree($tree){
  $tree2 = array();
  foreach($tree as $i => $item){
    $submenu = $item['submenu'];
    unset($item['submenu']);  //clear submenu of parent item
    $tree2[] = $item;
    if(!empty($submenu)){
      $sub = formatTree($submenu); //submenu's return as array in array
      $tree2[] = $sub[0]; // remove outer array
    }
  }
  return $tree2;
}

1 Comment

its not working for more than two level. for example -
0

Just try this,

$array = Array
(
    "58" => Array
    (
        "cat_id" => 58,
        "cat_name" => "Desserts",
        "cat_parent_id" => 0,
        "submenu" => Array
        (
            "535" => Array
            (
                "cat_id" => 535,
                "cat_name" => "dessert child",
                "cat_parent_id" => 58,
                "submenu" => Array
                ()

            )

        )

    ),

    "56" => Array
    (
        "cat_id" => 56,
        "cat_name" => "Biryani & Rice",
        "cat_parent_id" => 0,
        "submenu" => Array
        ()

    )
);

function singledimensional($array)
{
    $res = array();
    foreach ($array as $i => $item) {
        $temparr = $item;
        $item['submenu'] = array();
        $res[] = $item;
        if (!empty($temparr['submenu']) ){
              $child = singledimensional($temparr['submenu']);
              $res[] =  $child[0];
        }
    }
    return $res;
}

    echo '<pre>';
    print_r(singledimensional($array));
    echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [cat_id] => 58
            [cat_name] => Desserts
            [cat_parent_id] => 0
            [submenu] => Array
                (
                )

        )

    [1] => Array
        (
            [cat_id] => 535
            [cat_name] => dessert child
            [cat_parent_id] => 58
            [submenu] => Array
                (
                )

        )

    [2] => Array
        (
            [cat_id] => 56
            [cat_name] => Biryani & Rice
            [cat_parent_id] => 0
            [submenu] => Array
                (
                )

        )

)

I hope this will help you :)

Comments

0

(PHP 4 >= 4.0.1, PHP 5)

array_merge_recursive — Merge two or more arrays recursively

`function array_merge_recursive_distinct ( array &$array1, array &$array2 ) { $merged = $array1;

foreach ( $array2 as $key => &$value )
{
  if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array (     $merged [$key] ) )
  {
    $merged [$key] = array_merge_recursive_distinct ( $merged [$key],   $value );
}
else
{
  $merged [$key] = $value;
}

}

return $merged; } ?>`

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.