What are some strategies for ensuring that only relevant sub-links are displayed based on the selected "parent" link in a PHP navigation system?

When designing a PHP navigation system, it is important to ensure that only relevant sub-links are displayed based on the selected "parent" link. One way to achieve this is by using conditional statements to dynamically generate the sub-links based on the selected parent link. By checking the value of the parent link and displaying only the corresponding sub-links, you can create a more streamlined and user-friendly navigation experience.

<?php
// Define an array of parent links and their corresponding sub-links
$navigation = [
    'Home' => ['About', 'Contact'],
    'Products' => ['Product 1', 'Product 2', 'Product 3'],
    'Services' => ['Service 1', 'Service 2'],
];

// Get the selected parent link from the URL or user input
$selectedParent = isset($_GET['parent']) ? $_GET['parent'] : 'Home';

// Display the selected parent link
echo "<ul>";
echo "<li>$selectedParent</li>";

// Display the relevant sub-links based on the selected parent link
if (array_key_exists($selectedParent, $navigation)) {
    echo "<ul>";
    foreach ($navigation[$selectedParent] as $subLink) {
        echo "<li>$subLink</li>";
    }
    echo "</ul>";
} else {
    echo "<li>No sub-links found</li>";
}
echo "</ul>";
?>