Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 799287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:06:08+00:00 2026-05-14T23:06:08+00:00

The question: How do I generate navigation, allowing for applying different classes to different

  • 0

The question: How do I generate navigation, allowing for applying different classes to different sub-items, from a multi-dimensional array?

Here is how I was doing it before I had any need for multi-level navigation:

Home 
Pics 
About

and was generated by calling nav():

function nav(){       
    $links = array(
        "Home" => "home.php",
        "Pics" => "pics.php",
        "About" => "about.php"
    );

    $base = basename($_SERVER['PHP_SELF']);

    foreach($nav as $k => $v){
        echo buildLinks($k, $v, $base);
    }
}

Here is buildLinks():

function buildLinks($name, $page, $selected){
    if($selected == $page){
       $theLink = "<li class=\"selected\"><a href=\"$page\">$name</a></li>\n";
    } else {
       $thelink = "<li><a href=\"$page\">$name</a></li>\n";
    }

    return $thelink;
}

My question, again:

how would I achieve the following nav (and notice that the visible sub navigation elements are only present when on that specific page):

Home
    something1
    something2 
Pics 
About

and…

Home
Pics
    people
    places 
About

What I’ve tried

From looking at it it would seem that some iterator in the SPL would be a good fit for this but I’m not sure how to approach this. I have played around with RecursiveIteratorIterator but I’m not sure how to apply a different style to only the sub menu items and also how to only show these items if you are on the correct page.

I built this array to test with but don’t know how to work with the submenu1 items individually:

$nav = array(
array(
"Home" => "home.php",
"submenu1" => array(
    "something1"=>"something1.php",
    "something2" => "something2.php")
),
array("Pics" => "pics.php"),
array("About" => "about.php")
);

The following will print out the lot in order but how do I apply, say a class name to the submenu1 items or only show them when the person is on, say, the “Home” page?

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($nav));

foreach($iterator as $key=>$value) {
    echo $key.' -- '.$value.'<br />';
}

And this gets me:

Home
something1
something2
Pics
About

But I have no way to apply classes to those sub items and no way to only display them conditionally because I don’t see how to target just these elements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:06:09+00:00Added an answer on May 14, 2026 at 11:06 pm

    You were on the right track with RecursiveIteratorIterator. It essentially flattens a recursive iterator. Here is the correct way:

    $nav = array(
        array(
        "Home" => "home.php",
        "submenu1" => array(
            "something1"=>"something1.php",
            "something2" => "something2.php")
        ),
        array("Pics" => "pics.php"),
        array("About" => "about.php"),
    );
    
    $it = new RecursiveIteratorIterator(
        new RecursiveArrayIterator($nav),
        RecursiveIteratorIterator::SELF_FIRST
    );
    
    foreach ($it as $k => $v) {
        if ($it->getDepth() == 0)
            continue;
        echo str_repeat("    ", $it->getDepth() - 1) .
            "$k => $v\n";
    }
    

    gives

    Home => home.php
    submenu1 => Array
        something1 => something1.php
        something2 => something2.php
    Pics => pics.php
    About => about.php
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked this question before: Generate XML from a class I want to do
I want to generate Javascript code from an existing Java project ( original question
I have one question. I am using Apache CXF framework to generate model classes
Ok I've seen this question Entity Framework: Generate Database From Model removes Stored Procedures
This is a follow on from a previously posted question: How to generate a
This follows on from this question: Algorithm to generate spanning set Given this input:
I may not have enough information to generate a complete question here but I
This question is related to my previous question How to generate Cartesian Coordinate (x,y)
Question 1 We would like to generate a site map for our CMS site
This question is an exact duplicate of: Why generate long serialVersionUID instead of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.