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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决