duanli4146 2014-06-13 17:58
浏览 52
已采纳

使用PHP中的两个数组创建子菜单菜单

    Array
    (
        [0] => stdClass Object
            (
                [id] => 1
                [name] => Tops
                [parent_id] => 
            )

        [1] => stdClass Object
            (
                [id] => 2
                [name] => Trousers
                [parent_id] => 
            )

        [2] => stdClass Object
            (
                [id] => 3
                [name] => Dresses
                [parent_id] => 
            )

        [3] => stdClass Object
            (
                [id] => 4
                [name] => Skirts
                [parent_id] => 
            )

        [4] => stdClass Object
            (
                [id] => 5
                [name] => Accessories
                [parent_id] => 
            )

        [5] => stdClass Object
            (
                [id] => 6
                [name] => Coats & Jackets
                [parent_id] => 
            )

)

Above is the parent categories i fetch from db.

And below are the sub categories

 Array
(
    [0] => stdClass Object
        (
            [id] => 8
            [name] => Mini Tops
            [parent_id] => 1
        )

    [1] => stdClass Object
        (
            [id] => 9
            [name] => Long Tops
            [parent_id] => 2
        )

    [2] => stdClass Object
        (
            [id] => 10
            [name] => Cargo
            [parent_id] => 2
        )

    [3] => stdClass Object
        (
            [id] => 11
            [name] => Bermuda
            [parent_id] => 2
        )

    [4] => stdClass Object
        (
            [id] => 12
            [name] => Formal Dresses
            [parent_id] => 3
        )

    [5] => stdClass Object
        (
            [id] => 13
            [name] => Casual Dresses
            [parent_id] => 3
        )

    [6] => stdClass Object
        (
            [id] => 14
            [name] => Party Wear
            [parent_id] => 3
        )

    [7] => stdClass Object
        (
            [id] => 15
            [name] => Jewelry 
            [parent_id] => 4
        )

    [8] => stdClass Object
        (
            [id] => 16
            [name] => Bracelets
            [parent_id] => 4
        )

    [9] => stdClass Object
        (
            [id] => 17
            [name] => Caps
            [parent_id] => 4
        )

)

below is how i am making my navitaion bar

             <ul>
                <li>
                  <a href="search.html">Tops</a>
                  <ul class="sub-menu">
                        <li><a href="search.html">Tshirts</a></li>
                        <li><a href="search.html">Jumpers</a></li>
                        <li><a href="search.html">Cardigans</a></li>
                        <li><a href="search.html">Knitwear</a></li>
                    </ul>
                </li>
              </ul>

As my logics are not so much good i actually trying to make navitaion with these two arrays in a way that the first array which are main links should be on top order where as all respective childs will appear in submenu of there respective parent_id example

Long Top, Cargo and Bermuda should be falling under Trousers Link.

I need guidance how can i achieve this .. I tried foreach loop but stucked in middle

Thanks alot

  • 写回答

1条回答 默认 最新

  • dreinuqm992401 2014-06-13 18:15
    关注

    If you're in control you should try to use a different choice of arrays because now you will be doing an unnecessary amount of looping. To demonstrate:

    foreach($categories as $cat) {
        echo "<li>".$cat->name."<ul>";
        foreach($subcategories as $subcat) {
            if($subcat->parent_id == $cat->id) echo "<li>".$subcat->name."</li>"
        }
        echo "</ul></li>";
    }
    

    You have to loop over all subcategories for each category. But if you instead stored subcategories in an array where the index represented the parent ID, you could easily retrieve them.

    If you want to solve this with just one array you could make the list of subcategories a property of the corresponding parent category. This would be easy to handle and semantic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加