What is the best way to generate a dynamic menu in PHP using arrays?

To generate a dynamic menu in PHP using arrays, you can create a multi-dimensional array that represents the menu structure. Then, you can use a recursive function to loop through the array and output the menu items. This approach allows for easy customization and maintenance of the menu structure.

$menuItems = array(
    'Home' => '#',
    'About' => '#',
    'Services' => array(
        'Web Design' => '#',
        'Graphic Design' => '#',
        'Digital Marketing' => '#'
    ),
    '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($menuItems);