What are the differences between handling single-level and multi-level navigation arrays in PHP?

When handling single-level navigation arrays in PHP, you can simply loop through the array using a foreach loop to display the navigation items. However, when dealing with multi-level navigation arrays, you need to implement a recursive function to iterate through each level of the array and display the nested navigation items.

// Single-level navigation array
$navItems = ['Home', 'About', 'Services', 'Contact'];

// Loop through single-level navigation array
foreach ($navItems as $item) {
    echo '<a href="#">' . $item . '</a>';
}

// Multi-level navigation array
$navItems = [
    'Home',
    'About',
    'Services' => [
        'Web Design',
        'SEO',
        'PPC'
    ],
    'Contact'
];

// Recursive function to handle multi-level navigation array
function displayNavigation($navItems) {
    echo '<ul>';
    foreach ($navItems as $key => $value) {
        if (is_array($value)) {
            echo '<li>' . $key;
            displayNavigation($value);
            echo '</li>';
        } else {
            echo '<li><a href="#">' . $value . '</a></li>';
        }
    }
    echo '</ul>';
}

// Display multi-level navigation
displayNavigation($navItems);