How can you create a dynamic menu with submenus in PHP?

To create a dynamic menu with submenus in PHP, you can use multidimensional arrays to store the menu items and their corresponding submenus. By iterating through the array, you can generate the menu structure dynamically. You can then use HTML and CSS to style the menu and display it on your website.

<?php
$menu = array(
    'Home' => '#',
    'About' => '#',
    'Services' => array(
        'Web Design' => '#',
        'Graphic Design' => '#',
        'SEO' => '#'
    ),
    'Contact' => '#'
);

function generateMenu($items) {
    echo '<ul>';
    foreach ($items as $key => $value) {
        echo '<li><a href="' . $value . '">' . $key . '</a>';
        if (is_array($value)) {
            generateMenu($value);
        }
        echo '</li>';
    }
    echo '</ul>';
}

generateMenu($menu);
?>