I've created a simple PHP Array that I wish to use with a select input on a web form (the select input was previously hardcoded in the HTML). Here's the array:
$fruits = array(
'fruit' => 'apple',
'fruit' => 'orange',
'vegetable' => 'potato'
);
and here's the select input:
<label for="Food">Select the Food</label><br>
<select id="Subdiscipline" name="Subdiscipline">
<option value="*">- No Selection - </option>
<?php
$output = "";
$selected = false;
foreach($fruits as $fruit => $value) {
$fruit = htmlspecialchars($fruit);
$output .= "<option value=\"$fruit\"";
if ($fruit == $previousFruitSelection) {
$selected = true;
$output .= " selected";
}
$output .= ">$value</option>";
}
echo $output;
?>
</select>
The problem now that I'm no longer hardcoding the options for the select menu is that the option for 'apple' no longer appears, presumably because you can't have duplicate keys in the array. Is there a way around this to use PHP to create an array that is used to drive the select options but allow for multiple options with the same 'value=fruit'?