Jump to content

[SOLVED] PHP Array for Form Elements


Omzy

Recommended Posts

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'),
);

 

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>

 

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.

Link to comment
Share on other sites

I managed to figure it out (no thanks to "genericnumber1")...

 

foreach($attributes as $key1 => $value1)
{
echo '<select name="'.$key1.'">';

foreach($value1 as $key2 => $value2)
{
  echo '<option value="'.$key2.'">'.$value2.'</option>';
}

echo '</select>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.