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

时间:2014-06-13 17:58:37

标签: 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] => 
            )

)

以上是我从db中获取的父类别。

以下是子类别

 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
        )

)

下面是我如何制作我的导航栏

             <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>

由于我的逻辑并不是那么好,我实际上试图用这两个数组进行导航,使得作为主链接的第一个数组应该在顶级顺序,因为所有相应的子节点将出现在各自的parent_id的子菜单中例子

Long Top,Cargo和Bermuda应该属于Trousers Link。

我需要指导我怎样才能实现这一点..我尝试过foreach循环但是在中间

非常感谢

1 个答案:

答案 0 :(得分:1)

如果您处于控制之中,您应该尝试使用不同的数组选择,因为现在您将进行不必要的循环。为了证明:

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>";
}

您必须遍历每个类别的所有子类别。但是,如果您将子类别存储在索引表示父ID的数组中,则可以轻松地检索它们。

如果只想用一个数组解决这个问题,可以将子类别列表作为相应父类别的属性。这很容易处理和语义化。