What are the potential pitfalls of using a dynamic menu with multiple submenus in PHP?

One potential pitfall of using a dynamic menu with multiple submenus in PHP is the complexity of managing and displaying the menu structure. To solve this issue, you can use a recursive function to iterate through the menu items and their submenus, ensuring that the menu is displayed correctly at each level.

function displayMenu($menuItems) {
    echo '<ul>';
    foreach ($menuItems as $menuItem) {
        echo '<li>' . $menuItem['name'];
        if (!empty($menuItem['submenus'])) {
            displayMenu($menuItem['submenus']);
        }
        echo '</li>';
    }
    echo '</ul>';
}

$menu = [
    [
        'name' => 'Home',
        'submenus' => []
    ],
    [
        'name' => 'Products',
        'submenus' => [
            [
                'name' => 'Product 1',
                'submenus' => []
            ],
            [
                'name' => 'Product 2',
                'submenus' => []
            ]
        ]
    ]
];

displayMenu($menu);