How can recursion be implemented in PHP to handle a navigation structure with nested sub-links?

To handle a navigation structure with nested sub-links using recursion in PHP, you can create a function that iterates over each item in the navigation structure. If an item has children, the function can call itself recursively to process the children. This allows for a flexible and scalable way to handle nested sub-links within the navigation structure.

function displayNavigation($items) {
    echo '<ul>';
    foreach ($items as $item) {
        echo '<li><a href="' . $item['url'] . '">' . $item['label'] . '</a>';
        if (!empty($item['children'])) {
            displayNavigation($item['children']);
        }
        echo '</li>';
    }
    echo '</ul>';
}

$navigation = [
    ['label' => 'Home', 'url' => '/home'],
    ['label' => 'About', 'url' => '/about', 'children' => [
        ['label' => 'Team', 'url' => '/about/team'],
        ['label' => 'History', 'url' => '/about/history']
    ]],
    ['label' => 'Services', 'url' => '/services', 'children' => [
        ['label' => 'Web Development', 'url' => '/services/web'],
        ['label' => 'Graphic Design', 'url' => '/services/design']
    ]]
];

displayNavigation($navigation);