1

I'm trying to create dynamic multi level menus fetching the data from a MySQL DB, using PHP. I've managed to order the menu items in a php array with this format:

-----------------------
Array
(
[1] => Array
    (
        [id] => 1
        [ubicacion] => top_a
        [nivel] => 1
        [parent_id] => 
        [tipo] => link
        [link] => http://www.google.com
        [titulo] => Google
        [alias] => google_es
        [children] => Array
            (
                [3] => Array
                    (
                        [id] => 3
                        [ubicacion] => top_a
                        [nivel] => 2
                        [parent_id] => 1
                        [tipo] => link
                        [link] => http://www.gmail.com
                        [titulo] => Gmail
                        [alias] => gmail
                        [children] => Array
                            (
                                [4] => Array
                                    (
                                        [id] => 4
                                        [ubicacion] => top_a
                                        [nivel] => 3
                                        [parent_id] => 3
                                        [tipo] => link
                                        [link] => www.inbox.gmail.com
                                        [titulo] => Inbox
                                        [alias] => inbox_gmail
                                    )

                            )

                    )

            )
    )

[2] => Array
    (
        [id] => 2
        [ubicacion] => top_a
        [nivel] => 1
        [parent_id] => 
        [tipo] => link
        [link] => http://www.yahoo.com
        [titulo] => Yahoo
        [alias] => yahoo
    )
)
-----------------------

The problem is that I can't figure out how to output this array as HTML markup in a way that will work with n levels. I can do it with a fixed number of levels like this:

foreach($menu_array as $menu) {
 echo "<li><a href='{$menu['link']}'>{$menu['titulo']}</a>";
 if (array_key_exists('children',$menu)) {
    echo "<ul>";
    foreach ($menu['children'] as $child_menu) {
        echo "<li><a href='{$child_menu['link']}'>{$child_menu['titulo']}</a>";
        if (array_key_exists('children',$child_menu)) {
            echo "<ul>";
            foreach ($child_menu['children'] as $child2_menu) {
                echo "<li><a href='{$child2_menu['link']}'>{$child2_menu['titulo']}</a>";
            }
            echo "</ul>";
        }
    }
    echo "</ul>";
}
echo "</li>";
}

But this only works for 3 levels, and I know there should be a way to solve this issue, I know I'm not the first one facing a problem with HTML output of a multidimensional array.

2
  • would you mind giving as a bit of a background why you need the menus to be in the database? Commented Jul 9, 2012 at 23:10
  • I'm doing a custom CMS for future proyects Commented Jul 10, 2012 at 0:31

1 Answer 1

11

You can just use a little bit of recursion to get you to more levels.

function echo_menu($menu_array) {
    //go through each top level menu item
    foreach($menu_array as $menu) {
        echo "<li><a href='{$menu['link']}'>{$menu['titulo']}</a>";
        //see if this menu has children
        if(array_key_exists('children', $menu)) {
            echo '<ul>';
            //echo the child menu
            echo_menu($menu['children']);
            echo '</ul>';
        }
        echo '</li>';
    }
}

echo '<ul>';
echo_menu($menu_array);
echo '</ul>';

This will work for any number of child levels you'd like.

Sign up to request clarification or add additional context in comments.

8 Comments

Any number of level until you hit the recursion limit ;)
I would call that a feature. Nobody wants to go through that many nested menus! ;)
Thank you very much :) it works. I don't know how I couldn't think of this :P
I know people don't want to see 20 levels of menus :P but I think it's always good to cover all the possible scenarios when you are dealing with user input
what is considered in @menu_array ? Can you how it within example ? thanks :)
|

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.