php create navigation menu from multidimensional array dynamically

后端 未结 4 532
夕颜
夕颜 2021-01-01 07:38

I did research on this, and wasn\'t able to find an exact answer. Most of the questions/answers on here pertaining to this seem to be unfinished. If anyone knows of a fini

相关标签:
4条回答
  • 2021-01-01 07:43

    I would probably slightly adapt the array to be something like the following:

    Array(
        0 => Array(
            'title' => 'Home',
            'children' => Array()
        ),
        1 => Array(
            'title' => 'Parent',
            'children' => Array(
                0 => Array(
                    'title' => 'Sub 1',
                    'children' => Array(),
                ),
                1 => Array(
                    'title' => 'Sub 2',
                    'children' => Array(
                        0 => Array(
                            'title' => 'Sub sub 2-1',
                            'children' => Array(),
                        ),
                    ),
                ),
            )
        )
    ) 
    

    With a structure like this you could use recursion to build your menu HTML:

    function buildMenu($menuArray)
    {
        foreach ($menuArray as $node)
        {
            echo "<li><a href='#'/>" . $node['title'] . "</a>";
            if ( ! empty($node['children'])) {
                echo "<ul>";
                buildMenu($node['children']);
                echo "</ul>";
            }
            echo "</li>";
        }
    }
    
    0 讨论(0)
  • 2021-01-01 07:49

    Calvin's solution worked for me. Here's the edited version. We can use more nested loops to get sub - sub menu items.

    echo '<ul>';
    foreach ($menu as $parent) {
    
        echo '<li><a href="#">' . $parent . '</a>';
    
        if (is_array($parent)) {
            echo '<ul>';
                foreach ($parent as $children) {
                    echo '<li><a href="#">' . $children . '</a>';
                }
            echo '</ul>';
        }
    
        echo '</li>';
    }
    echo '</ul>';
    0 讨论(0)
  • 2021-01-01 07:52

    I think you can use recursion? Here is some pseudocode, not very familiar with php.

    function toNavMenu(array A){
        for each element in A{
            echo "<li><a href=\"\">" + element.name + "</a>"
            if (element is an array){
                echo "<ul>"
                toNavMenu(element)
                echo "</ul>"
            }
            echo "</li>"
        }
    }
    
    0 讨论(0)
  • 2021-01-01 07:59

    Here is my solution:

    <?php
    
    function MakeMenu($items, $level = 0) {
        $ret = "";
        $indent = str_repeat(" ", $level * 2);
        $ret .= sprintf("%s<ul>\n", $indent);
        $indent = str_repeat(" ", ++$level * 2);
        foreach ($items as $item => $subitems) {
            if (!is_numeric($item)) {
                $ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $item);
            }
            if (is_array($subitems)) {
                $ret .= "\n";
                $ret .= MakeMenu($subitems, $level + 1);
                $ret .= $indent;
            } else if (strcmp($item, $subitems)){
                $ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $subitems);
            }
            $ret .= sprintf("</li>\n", $indent);
        }
        $indent = str_repeat(" ", --$level * 2);
        $ret .= sprintf("%s</ul>\n", $indent);
        return($ret);
    }
    
    $menu = Array(
                'home' => Array("sub-home1", "sub-home2"),
                'about' => Array("sub-about", "about2" => Array("sub-sub-about")),
                'staff' => Array("sub-staff1", "sub-staff2"),
                'contact' => "contact"
            );
    
    print_r($menu);
    
    echo MakeMenu($menu);
    
    ?>
    
    0 讨论(0)
提交回复
热议问题