How can PHP handle nested dropdown options efficiently?

To efficiently handle nested dropdown options in PHP, you can use multidimensional arrays to represent the hierarchical structure of the options. By organizing the options in nested arrays, you can easily iterate through them to generate the dropdown HTML code. This approach allows for a flexible and scalable solution to handle nested dropdown options efficiently.

$options = [
    'Option 1' => [
        'Suboption 1.1',
        'Suboption 1.2',
    ],
    'Option 2' => [
        'Suboption 2.1',
        'Suboption 2.2',
    ],
];

echo '<select>';
foreach ($options as $key => $value) {
    echo '<option>' . $key . '</option>';
    if (is_array($value)) {
        foreach ($value as $suboption) {
            echo '<option>-- ' . $suboption . '</option>';
        }
    }
}
echo '</select>';