How can arrays be effectively utilized in PHP to compare and display selected values in dropdown menus?
To compare and display selected values in dropdown menus using arrays in PHP, you can create an array that contains the options for the dropdown menu and then loop through the array to display the options. You can use an if statement to check if a specific value is selected and mark it as selected in the dropdown menu.
<?php
$options = array("Option 1", "Option 2", "Option 3");
$selectedValue = "Option 2";
echo "<select>";
foreach ($options as $option) {
if ($option == $selectedValue) {
echo "<option value='$option' selected>$option</option>";
} else {
echo "<option value='$option'>$option</option>";
}
}
echo "</select>";
?>