What are some recommended methods for creating multi-layered navigation menus in PHP?

Creating multi-layered navigation menus in PHP can be achieved by using recursive functions to iterate through nested menu items. One common approach is to store menu items in a multidimensional array where each item can have sub-items. By recursively looping through the array, we can generate the HTML markup for the navigation menu with multiple levels.

function buildMenu($menuItems) {
    $html = '<ul>';
    foreach ($menuItems as $item) {
        $html .= '<li><a href="' . $item['url'] . '">' . $item['label'] . '</a>';
        if (isset($item['children']) && !empty($item['children'])) {
            $html .= buildMenu($item['children']);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}

$menu = array(
    array(
        'label' => 'Home',
        'url' => '/',
    ),
    array(
        'label' => 'About',
        'url' => '/about',
    ),
    array(
        'label' => 'Services',
        'url' => '/services',
        'children' => array(
            array(
                'label' => 'Web Development',
                'url' => '/services/web-development',
            ),
            array(
                'label' => 'Graphic Design',
                'url' => '/services/graphic-design',
            ),
        ),
    ),
);

echo buildMenu($menu);