简体   繁体   中英

building recursive navigation menu list

I've $menu_array3 as

Array
(
    [9] => Array
        (
            [category_name] => cat1
            [category_id] => 9
            [children] => Array
                (
                    [12] => Array
                        (
                            [category_name] => test5
                            [category_id] => 12
                        )

                    [10] => Array
                        (
                            [category_name] => cat2
                            [category_id] => 10
                            [children] => Array
                                (
                                    [15] => Array
                                        (
                                            [category_name] => cat7
                                            [category_id] => 15
                                            [children] => Array
                                                (
                                                    [18] => Array
                                                        (
                                                            [category_name] => cat10
                                                            [category_id] => 18
                                                        )

                                                    [16] => Array
                                                        (
                                                            [category_name] => cat8
                                                            [category_id] => 16
                                                        )

                                                )

                                        )

                                    [17] => Array
                                        (
                                            [category_name] => cat9
                                            [category_id] => 17
                                        )

                                )

                        )

                )

        )

    [11] => Array
        (
            [category_name] => cat3
            [category_id] => 11
            [children] => Array
                (
                    [13] => Array
                        (
                            [category_name] => cat5
                            [category_id] => 13
                        )

                )

        )

)

with ref to this answer I tried to build navigation menu using

function build_nav($category_name, $data)
{
    $result = array();

    foreach ($data as $row)
    {
        if ($row['category_name'] == $category_name)
        {
            $result = "<li>" . $row['category_name'] . "</li>"; 
            $result= build_nav($row['category_name'], $data);
        }
    }
    return $result;
}
$menu="<ul>";
$menu.=build_nav('cat1', $menu_array3);
$menu.="</ul>";
echo "menu <pre>"; print_r($menu); echo "</pre>";

But i was stopped by

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 261900 bytes)

I request you to correct me with suggestions. Thanks in advance Edit I found solution to build navigation menu directly from db at https://stackoverflow.com/a/3380296/1528701

The reason for this error may be that you are calling your function build_nav() inside the foreach loop. This means that for every value in data, build_nav() will be trigered in which the foreach loop will again be triggerd and so on. So the main answer is : You've created an infinite loop.

I think you may have misinterpreted the answer which inspired you to make this function. In that case they are using a parent-id. This may help you in your search. Also notice that they don't use a deeply nested array like you are using. Take a good look and you'll find your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM