简体   繁体   English

建立递归导航菜单列表

[英]building recursive navigation menu list

I've $menu_array3 as 我有$menu_array3作为

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 在此先感谢编辑我找到解决方案,直接从DB构建导航菜单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. 出现此错误的原因可能是您在foreach循环中调用了函数build_nav()。 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. 这意味着对于数据中的每个值,都会触发build_nav(),其中将再次触发foreach循环,依此类推。 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. 在这种情况下,他们使用的是父代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. 好好看看,您会找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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