In what ways can PHP scripts be modified to display only a specific subtree of a menu structure based on user interaction, such as clicking on a particular menu item?

To display only a specific subtree of a menu structure based on user interaction, such as clicking on a particular menu item, you can use JavaScript to handle the user interaction and send an AJAX request to a PHP script that generates the specific subtree based on the user's selection. The PHP script can then return the subtree HTML content to be displayed on the page dynamically.

<?php
// menu.php - PHP script that generates the menu structure

// Define the menu structure as an array
$menu = array(
    'Home' => array(
        'About Us',
        'Contact Us'
    ),
    'Products' => array(
        'Product 1',
        'Product 2',
        'Product 3'
    )
);

// Check if a specific submenu is requested
if(isset($_GET['submenu'])) {
    $submenu = $_GET['submenu'];
    if(array_key_exists($submenu, $menu)) {
        foreach($menu[$submenu] as $item) {
            echo '<li>' . $item . '</li>';
        }
    } else {
        echo 'Submenu not found';
    }
} else {
    // Display the full menu structure
    foreach($menu as $key => $value) {
        echo '<li>' . $key . '</li>';
    }
}
?>