Php用于导航的多维数组

时间:2014-04-03 21:20:07

标签: php html codeigniter

需要的导航HTML              

  • 主页
  •         
  • 页             
                      
    • 关于
    •                 
    • 服务
    •                 
    • 产品
    •                 
    • 联系
    •                 
    • 常见问题
    •                 
    • 网页
    •                 
    • 隐私政策
    •                 
    • 列布局                     
                                
      • 1栏
      •                         
      • 2列(左侧边栏)
      •                         
      • 2列(右侧边栏)
      •                         
      • 3栏
      •                         
      • 4栏
      •                     
                      
    •             
            
  •     

    我想使用php数组和foreach循环来输出所需的html。 我到目前为止的PHP代码是:

    <?php
        $data = array("navigation");
        $data['navigation']['Home'] = base_url();
        $data['navigation']['Pages'] = base_url('pages');
        $data['navigation']['Pages']['About'] = base_url('pages/about');
        echo '<ul>';
        foreach($data as $nav) {
            foreach($nav as $subNavKey => $subNavHref) {
                echo "<li><a href='$subNavHref'>$subNavKey</a>";
            }
        }
        echo '</ul>';
    ?>
    

    我在想我需要嵌套三个foreach循环但是在第三个循环到达时会产生php警告/错误:

    $data['navigation']['Home'] = base_url();
    $data['navigation']['Pages'] = base_url('pages');
    

    我不太确定如何测试第三级深度,例如:

    $data['navigation']['Pages']['About'] = base_url('pages/about');
    

    此外,在正确的位置输出所需的li和ul标签也给我带来了麻烦。

    4 个答案:

    答案 0 :(得分:2)

    使用递归

    $data['navigation']['Home'] = base_url();
    $data['navigation']['Pages'] = base_url('pages');
    $data['navigation']['Pages']['About'] = base_url('pages/about');
    $data['navigation']['Pages']['About']['Team'] = base_url('pages/team');
    $data['navigation']['Pages']['About']['Team']['Nate'] = base_url('pages/nate');
    
    echo "<ul>"
    print_list($data);
    echo "</ul>"
    
    function print_list($menu) {
      foreach($menu as $key=>$item) {
        echo "<li>";
        if(is_array($item)) {
          echo "<ul>";
          print_list($item);
          echo "</ul>";
        } else {
          echo "<a href='{$val}'>$key</a>";
        }
        echo "</li>";
      }
    }
    

    答案 1 :(得分:2)

    <?php
    
    function nav($data) {
        $html = '<ul>';
        foreach ($data as $k => $v) {
            if (is_array($v)) {
                $html .=  "<li>$k" . nav($v) . "</li>";
            }
            else {
                $html .= "<li><a href='$k'>$v</a>";
            }
        }
        $html .= '</ul>';
        return $html;
    }
    
    echo nav($data);
    

    答案 2 :(得分:1)

    递归函数可以完成工作:

    $items = array(
        "Home",
        "Pages" => array(
            "About",
            "Services",
            "Products",
            "Contact",
            "FAQs",
            "Sitemap",
            "Privacy Policy",
            "Column Layouts" => array(
                "1 Column",
                "2 Column (Left Sidebar)",
                "2 Column (Right Sidebar)",
                "3 Column",
                "4 Column"
            )
        )
    );
    
    function getMenu($array) {
        foreach($array as $key => $value) {
            if(is_array($value)) {
                echo "<li>" . $key . "</li>";
                echo "<ul>";
                getMenu($value);
                echo "</ul>";
            } else {
                echo "<li>" . $value . "</li>";
            }
        }
    }
    echo "<ul>";
    getMenu($items);
    echo "</ul>";
    

    输出:

    enter image description here

    答案 3 :(得分:1)

    您应该使用递归函数,例如(Working Demo):

    function makeMenu($array)
    {
        $menu = '';
        foreach($array as $key => $value) {
            if(is_array($value)) {
                $menu .= '<li>' . $key . '<ul>' . makeMenu($value) . '</ul></li>';
            }
            else {
                $menu .= "<li><a href='". $value ."'>" . $value ."</a></li>";
            }
        }
        return $menu;
    }
    

    然后称之为:

    $data = array(
        "Home",
        "Pages" => array("About", "Services"),
        "Column Layouts" => array("1 Column", "2 Column (Left Sidebar)")
    );
    echo '<ul>' . makeMenu($data) . '</ul>';