0

I want to create dynamic menu with looping an array be 1 object menu. But error occured. Our code is below it:

$menus = [{"id" => 1, "label" => "content", "parent_id" => 0},{"id" => 2, "label" => "inbox", "id" => 3, "parent_id" => 0}, {"id" => 4, "label" => "item", "parent_id" => 0}];
$sub_menus = [{"id" => 5, "label" => "banner", "parent_id" => 1},{"id" => 6, "label" => "ads", "parent_id" => 1}];

foreach($menus as $row => $value){
    $nav[$row] = $value;
    foreach($sub_menus as $r => $v) {
        if($v['parent_id'] == $value['id']){
            $nav[$row]['sub_menu'][$r] = $v;
        }
     }
 }

I get error notif, "Indirect modification of overloaded element of App\Menu has no effect"

Please Help me :)

4
  • how do you get menus and sub_menus? show the code above Commented Apr 12, 2018 at 11:04
  • $menus,$sub_menus both are not php array, instead of {} write [] Commented Apr 12, 2018 at 11:08
  • Provide a sample of your expected output , So that we can understand better. Commented Apr 12, 2018 at 11:10
  • issue in the first line. You have 2 "id"s. {"id" => 2, "label" => "inbox", "id" => 3, "parent_id" => 0}, Commented Apr 12, 2018 at 11:16

2 Answers 2

3

The code is working. You have a lot of bugs in your arrays.

Fixed:

$menus = [
            [
                "id" => 1,
                "label" => "content",
                "parent_id" => 0
            ],
            [
                "id" => 2,
                "label" => "inbox",
                "parent_id" => 0
            ],
            [
                "id" => 4,
                "label" => "item",
                "parent_id" => 0
            ]
        ];

        $sub_menus = [
            [
                "id" => 5,
                "label" => "banner",
                "parent_id" => 1
            ],
            [
                "id" => 6,
                "label" => "ads",
                "parent_id" => 1
            ]
        ];

        foreach($menus as $row => $value){
            $nav[$row] = $value;
            foreach($sub_menus as $r => $v) {
                if($v['parent_id'] == $value['id']){
                    $nav[$row]['sub_menu'][$r] = $v;
                }
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

yes it work, I have failed because in my laravel framework I pull a data from database not use referense toArray. Thanks Mike :)
0

You have a broken array, something like an incorrect conversion of json to an array;

You can test(execute) it here (working example)

1 Comment

yes it work, I have failed because in my laravel framework I pull a data from database not use referense toArray. Thanks Overals. Big thanks for your suggest about your link wtools.io :D its Awesome !!! I use it now

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.