1

My existing array like this

$menus = array(
        0 =>array(
                "id"=> 1,
                "name"=> "A",
                "parent_id"=> 0
            ),
         1 =>array(
                "id"=> 2,
                "name"=> "B",
                "parent_id"=> 1
            ),
         2 =>array(
                "id"=> 3,
                "name"=> "C",
                "parent_id"=> 1
            ),
         3 =>array(
                "id"=> 4,
                "name"=> "D",
                "parent_id"=> 2
            ),
        4 =>array(
                "id"=> 5,
                "name"=> "E",
                "parent_id"=> 0
            ),

     );

I want to create that array like below output.

Expected Array

Array
(
    [0] => Array
        (
            [Menu] => Array
                (
                   [0] => Array
                          ( [name] => A
                            [Sub_menu] => Array
                             (
                                  [0] => Array
                                      (
                                        [name] => B
                                      ),
                                  [1] => Array
                                     (
                                        [name] => C
                                        [Sub_menu] => Array
                                          (
                                              [0] => Array
                                              (
                                                [name] => D
                                              ),

                                          )
                                     )
                             )
                          ),
                   [1] => Array
                          ( [name] => E

                          )
                )
         )
)

I tried some code and those are not working . my work output was like this image enter image description here

which repeating again in foreach loop in menu.

3
  • 1
    Great that you want to achieve that. but what about your effort? show us what you have tried so for? Commented Apr 20, 2016 at 4:39
  • @Anant i placed my php code though there some different because I worked on MVC platform. Commented Apr 20, 2016 at 4:47
  • Andrew's got it - see his answer. Commented Apr 20, 2016 at 5:18

2 Answers 2

3

You can use this function to construct menu recursively.

function makeMenu($items, $parentId)
{
    $menu = array_filter($items, function ($item) use ($parentId) {
        return $item['parent_id'] == $parentId;
    });
    foreach ($menu as &$item) {
        $subItems = makeMenu($items, $item['id']);
        if (!empty($subItems)) {
            $item['sub_menu'] = $subItems;
        }
    }
    return $menu;
}

Call it with $parentId = 0 and it will do work.

$readyMenu = makeMenu($itemsArray, 0);
Sign up to request clarification or add additional context in comments.

Comments

0

This code will work for you, Try...

$all = array(
        0 =>array(
                "id"=> 1,
                "name"=> "A",
                "parent_id"=> 0
            ),
         1 =>array(
                "id"=> 2,
                "name"=> "B",
                "parent_id"=> 1
            ),
         2 =>array(
                "id"=> 3,
                "name"=> "C",
                "parent_id"=> 1
            ),
         3 =>array(
                "id"=> 4,
                "name"=> "D",
                "parent_id"=> 2
            ),
        4 =>array(
                "id"=> 5,
                "name"=> "E",
                "parent_id"=> 0
            ),
    );  

foreach($all as $key => $val)
{
    if($val['parent_id']==0)
    {
        $data[]=$val;

        foreach($all as $k => $v)
        { 
            if($val['id'] == $v['parent_id']){
                $data[$key]['sub_menu'][]= $v;
                foreach($all as $a => $s)
                { 
                    if($v['id'] == $s['parent_id']){
                        $data[$key]['sub_menu'][$key]['sub_menu'][]= $s;
                    }
                }
            }
        }
    }
}
echo "<pre>"; print_r($data);
?

This will output like this :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => A
            [parent_id] => 0
            [sub_menu] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => B
                            [parent_id] => 1
                            [sub_menu] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [name] => D
                                            [parent_id] => 2
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 3
                            [name] => C
                            [parent_id] => 1
                        )

                )

        )

    [1] => Array
        (
            [id] => 5
            [name] => E
            [parent_id] => 0
        )

)

4 Comments

look you have changed array name to $All, it is $all everywhere in my code...
thats why it shows the warning Invalid argument supplied for foreach(), change $All to $all
Thiis is working perfectly @LemonKazi https://eval.in/556636
@ManjeetBarnala although your code works, but i suggest both you and OP to look andrew answer. Accurate+fast without any limitations.Thanks.

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.