[thelist] getting selective variables out of an array()

Bob Meetin bobm at dottedi.biz
Mon Jan 11 11:20:13 CST 2010


This is undoubtedly a no-brainer, but how do you do this:

<?php
$states = array(
array ( "abbr" => "AL", "name" => "Alabama" ),
array ( "abbr" => "AK", "name" => "Alaska" ),
array ( "abbr" => "AZ", "name" => "Arizona" )
)

foreach ($states as $key => $element )
{
  foreach ($element as $item)
  {
    print "$item<br />"; // this prints both $abbr ad $name
    // How do I print or set a variable for each item, abbr vs name?
  }
}
?>

I want to be able to populate a menu select menu like this:

<p>
<label for="state">US State or Canada</label>
<select name="state" id="state" size="1">
       <option value="AL" <?php if ($state == "AL" ) { echo selected; } 
?>>Alabama</option>
       <option value="AK" <?php if ($p_state == "AK" ) { echo selected; 
} ?>>Alaska</option>
</select>
</p>
   



More information about the thelist mailing list