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

Bob Meetin bobm at dottedi.biz
Mon Jan 11 12:40:28 CST 2010


Fred Jones wrote:
>> <?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?
>>  }
>> }
>> ?>
>>     
>
> Switch your foreache statements  and then use:
>
>  print "$key $element";
>   
Thanks but unless I misconstrued the instructions, this doesn't work:

see: http://www.dottedi.biz/codesamples/arrays

Both .php and .txt version

-Bob



More information about the thelist mailing list