You are here: Re: [PHP] traversing multi-dimensional arrays « PHP « IT news, forums, messages
Re: [PHP] traversing multi-dimensional arrays

Posted by Richard Lynch on 02/17/05 20:17

blackwater dev wrote:
> What is the best way to do this? I have two arrays:
>
> $cars=array("ford","chevy");
> $models=array("ford"=>array("ranger","escape"),"chevy"=>array("malibu","tahoe"));
>
> then I need to traverse both arrays:
>
> foreach ($cars as $car){
> //now I need to get into the models array and echo out all of the
> models that
> //coincide with the specific car so it should print
> out ford-ranger
> //I basically want to do this but can't
> //how can I get into this second array without looping
> through the whole thing each time?
> foreach($cars[$car] as $model){

You were very close:

foreach ($models[$car] as $model){


Note that everybody else's post that you shouldn't even NEED/USE the $cars
array was correct -- unless you've got some other reason to keep it around
and you *DO* need it, for reasons too complicated to put in the original
email.

>
> }
>
> }


--
Like Music?
http://l-i-e.com/artists.htm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация