2

I've got this multidimensional array to display a menu that my website uses in some PHP code. I've been doing some experimenting and reading and I want to get this to work with a multidimensional array so that I can dynamically display the corresponding menu.

I have 3 different menus that can be displayed, "one", "two", and "three".

$pages = array(
        'one' => array(
            'about 1' => array('services 1', 'partners 1'), 
            'events 1', 
            'contact 1'
        ), 
        'two' => array(
            'about 2' => array('services 2', 'partners 2'), 
            'events 2', 
            'contact 2'
        ), 
        'three' => array(
            'about 3' => array('services 3', 'partners 3'), 
            'events 3', 
            'contact 3'
        )
    );

I have tried a few things so far, and I can't figure this out - I think my multidimensional array needs to be changed around?

Basically if $current_page == 'one' then I want to echo the corresponding array for 'one'.

It would have to be something like this:

<?php
    echo '<ul class="menu">';
    if ($current_page == 'one'){
        foreach($pages['one'] as $page => $dropdown){
            if (array has third dimension){ // pseudo code if pages have third dimension
                echo '<li class="has-dropdown">' $page '</li>\n';
                echo '<ul class="dropdown">';
                foreach($pages['one'] as $page => $dropdown){
                    echo '<li>' . $dropdown . '</li>\n';
                }
                echo '</ul>';
            } else { // no third dimension
                foreach($pages['one'] as $page){
                    echo '<li>' $page '</li>\n';
                }
            }
        }
    }
    elseif ($current_page == 'two'){
        foreach($pages['two'] as $page){
            if (array has third dimension){ // pseudo code if pages have third dimension
                echo '<li class="has-dropdown">' $page '</li>\n';
                echo '<ul class="dropdown">';
                foreach($pages['one'] as $page => $dropdown){
                    echo '<li>' . $dropdown . '</li>\n';
                }
                echo '</ul>';
            } else { // no third dimension
                foreach($pages['one'] as $page){
                    echo '<li>' $page '</li>\n';
                }
            }
        }
    }
    elseif ($current_page == 'three'){
        foreach($pages['three'] as $page){
            if (array has third dimension){ // pseudo code if pages have third dimension
                echo '<li class="has-dropdown">' $page '</li>\n';
                echo '<ul class="dropdown">';
                foreach($pages['one'] as $page => $dropdown){
                    echo '<li>' . $dropdown . '</li>\n';
                }
                echo '</ul>';
            } else { // no third dimension
                foreach($pages['one'] as $page){
                    echo '<li>' $page '</li>\n';
                }
            }
        }
    }
    echo '</ul>';
?>

The above doesn't work, it's just a rough picture of what I've been trying. The question is really: how do I use a foreach loop to display my menu? Any help is appreciated!

1 Answer 1

2

I don't really know what to say, it's just code. Study it and ask if you have any questions, I'll be glad to help!

Note that this code will only handle 3 dimensional arrays, if you need to add more sub-levels it's a good idea to look into recursion but it may be a more complicated topic.

$pages = array(
    'one' => array(
        'about 1' => array('services 1', 'partners 1'),
        'events 1',
        'contact 1'
    ),
    'two' => array(
        'about 2' => array('services 2', 'partners 2'),
        'events 2',
        'contact 2'
    ),
    'three' => array(
        'about 3' => array('services 3', 'partners 3'),
        'events 3',
        'contact 3'
    )
);

$current_page = 'one';

echo '<ul class="menu">';
if (array_key_exists($current_page, $pages)) {
    foreach($pages[$current_page] as $item => $items) {
        echo '<li class="has-dropdown">';


        if (is_array($items)) {
            echo $item . '<ul class="dropdown">';
            foreach($items as $page => $dropdown) {
                echo '<li>'.$dropdown.
                '</li>\n';
            }
            echo '</ul>';
        }else{
            echo $items;
        }

        echo '</li>';
    }
}

echo '</ul>';
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! I've tested it here sandbox.onlinephpfunctions.com/code/… and played around with it a little, you're awesome! I don't have any questions atm though, but I will definitely look into recursion
I do have a question for you now! Is it possible to put that into a function and then call the function like this... $navigation = myMenuFunction(); echo $navigation; ?
@Matthew well of course. You'll need to consider the scope of the function. Store the html in a variable instead of echo and then return that variable from the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.