How can the code be optimized to efficiently handle multiple datasets in the dropdown selection process?

To efficiently handle multiple datasets in the dropdown selection process, we can use an associative array to store the datasets and dynamically generate the dropdown options based on the selected dataset. This allows us to easily switch between different datasets without duplicating code or creating multiple dropdowns.

<?php

// Define multiple datasets as associative arrays
$datasets = [
    'dataset1' => ['Option 1', 'Option 2', 'Option 3'],
    'dataset2' => ['Option A', 'Option B', 'Option C'],
    'dataset3' => ['Item X', 'Item Y', 'Item Z']
];

// Get the selected dataset from the dropdown
$selectedDataset = isset($_POST['dataset']) ? $_POST['dataset'] : 'dataset1';

// Generate dropdown options based on the selected dataset
echo '<select name="dataset">';
foreach ($datasets as $key => $options) {
    $selected = ($key == $selectedDataset) ? 'selected' : '';
    echo '<option value="' . $key . '" ' . $selected . '>' . $key . '</option>';
}
echo '</select>';

// Display the options for the selected dataset
echo '<select name="options">';
foreach ($datasets[$selectedDataset] as $option) {
    echo '<option>' . $option . '</option>';
}
echo '</select>';

?>