How can a PHP developer effectively handle the creation and organization of submenus in a dynamic menu system?

To effectively handle the creation and organization of submenus in a dynamic menu system, a PHP developer can use multidimensional arrays to represent the menu structure. By nesting arrays within arrays, each submenu can be easily associated with its parent menu item. This allows for a flexible and scalable way to build dynamic menus with multiple levels of depth.

$menu = array(
    'Home' => '#',
    'About' => '#',
    'Services' => array(
        'Web Development' => '#',
        'Mobile App Development' => '#',
        'UI/UX Design' => '#'
    ),
    'Contact' => '#'
);

// Loop through the menu array to display the menu items
foreach ($menu as $key => $value) {
    if (is_array($value)) {
        echo '<li>' . $key;
        echo '<ul>';
        foreach ($value as $subkey => $subvalue) {
            echo '<li>' . $subkey . '</li>';
        }
        echo '</ul>';
        echo '</li>';
    } else {
        echo '<li>' . $key . '</li>';
    }
}