What is the advantage of using recursion in PHP for rendering nested navigations?

When rendering nested navigations in PHP, using recursion allows for a more elegant and efficient way to handle the rendering of multiple levels of nested menus. This approach simplifies the code by allowing the same logic to be applied to each level of the navigation hierarchy, reducing redundancy and making the code easier to maintain.

function renderNavigation($items) {
    echo '<ul>';
    foreach ($items as $item) {
        echo '<li>' . $item['label'];
        if (!empty($item['children'])) {
            renderNavigation($item['children']);
        }
        echo '</li>';
    }
    echo '</ul>';
}

$navigation = [
    [
        'label' => 'Home',
        'children' => []
    ],
    [
        'label' => 'About',
        'children' => [
            [
                'label' => 'Team',
                'children' => []
            ],
            [
                'label' => 'History',
                'children' => []
            ]
        ]
    ],
    [
        'label' => 'Services',
        'children' => [
            [
                'label' => 'Web Design',
                'children' => []
            ],
            [
                'label' => 'SEO',
                'children' => []
            ]
        ]
    ]
];

renderNavigation($navigation);