Nate
Nate

Reputation: 259

Php Multidimensional array for navigation

Needed Navigation Html

  • Home
  • Pages
    • About
    • Services
    • Products
    • Contact
    • FAQs
    • Sitemap
    • Privacy Policy
    • Column Layouts
      • 1 Column
      • 2 Column (Left Sidebar)
      • 2 Column (Right Sidebar)
      • 3 Column
      • 4 Column
  • I want to use php arrays and foreach loops to output the needed html. The php code I have thus far is:

    <?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>';
    ?>
    

    I was thinking I would need three foreach loops nested but php warnings/errors are generated when the third loop is reached on lines such as:

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

    I'm not quite sure how to test for 3rd level depths such as:

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

    Also, outputting the needed li and ul tags in the proper positions has given me trouble aswell.

    Upvotes: 0

    Views: 4935

    Answers (4)

    Steve Robbins
    Steve Robbins

    Reputation: 13812

    <?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);
    

    Upvotes: 2

    The Alpha
    The Alpha

    Reputation: 146191

    You should use a recursive function, for example (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;
    }
    

    Then call it like:

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

    Upvotes: 1

    Ryan
    Ryan

    Reputation: 14649

    A recursive function can get the job done:

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

    Output:

    enter image description here

    Upvotes: 1

    Rahul Prasad
    Rahul Prasad

    Reputation: 8222

    Use recursion

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

    Upvotes: 4

    Related Questions