I'm creating a form and want some drop down boxes generated from an array: I want to do something like this: $attributes=array( 'location'=>array('manchester'=>'Manchester', 'london'=>'London'), ); Code (markup): Essentially what I want the code to do is output the following: <select name="location"> <option value="manchester">Manchester</option> <option value="london">London</option> </select> Code (markup): I've got the code to match on the outer array but I now need a foreach loop which will output the values from the inner array. I want to do this using one main array ($attributes) only.
<select name="blaha"> <option>$array['manchester']</option> <option>$array['london']</option> </select> maybe something like this ? sorry didnt read the beloiw bit ! just noticed what u need
$location = array(1,2,3); echo '<select name="location">'; foreach ($location as $value) { echo" <option>$value</option>"; } echo '</select>'; something like this may help. Just did it quicklyu to give you a quick idea on how to use the foreach as u wanted.
print "<select name="location">"; foreach($attributes as $loc) { print "<option>$loc</option>"; } print "</select>";
But that's not what I want it to do... <option value="manchester">Manchester</option> Notice the capitalization of the value attribute, I need this data to be pulled from the array. I could have a different word altogether for the value so it's not gonna be a simple case of using a string function.
I managed to figure it out... foreach($attributes as $key1 => $value1) { echo '<select name="'.$key1.'">'; foreach($value1 as $key2 => $value2) { echo '<option value="'.$key2.'">'.$value2.'</option>'; } echo '</select>'; } Code (markup):