Nesting functions

I am designing a script to generate a navigation menu. I have a function that goes through the items in a given list and formats them as a navigation list with links, etc.

For the items that are tagged as having a sub menu, the function generates a submenu from items associated with the submenu tag from a table.

It is possible for submenus to have submenus as well.

One way to generate the full list, submenus and all, is to run the list generating function including an if conditional to look for a submenu and run it again for the submenu. Each time the function is run it would generate the list at that level, generating submenus for tagged items.

If I generate the menus this way, I have to nest the function as many times as it is possible there may be a submenu. This is not too much of an issue as there are only likely to be 2-3 levels of submenus at the most.

However, I was wondering if there were a more elegant solution where the function would iterate itself when there was a submenu at the level at which it is working, without having to nest the function over and over.

I hope this question makes sense. It seems like there should be a solution, but I haven’t been able to find it.

I’ve imagined something like generating the primary list into an array and then apply array_walk(), somehow, which doesn’t really work as the list and all sublists would have to be in the array, which means generating them to begin with.

I hope this question makes sense.

Thanks,

–Kenoli

The correct term here is not “nesting”, but “recursion”. A very simple example of this would be:

$menu=array(
  'items'=>array(
     array('Item 1'),
     array('Item 2', 'items'=>array(
         array('Item 2.1'),
         array('Item 2.2'),
     )),
     array('Item 3'),
  ),
);

function createMenu($array)
{
    $str='<ul>';
    foreach($array as $item)
    {
        $str .= '<li>'.$item[0];
        if (isset($item['items']))
        {
            $str .= createMenu($item['items']); // function calling itself: this is known as recursion
        }
        $str .= '</li>';
    }
    $str .= '</ul>';
    return $str;
}

echo createMenu($menu['items']);

Great. Thanks. Now it looks painfully simple. I appreciate your taking the time to offer such a clear mock example (and kind correction re terminology).

–Kenoli