Re: traversing multi-dimensional arrays

From: Date: Thu, 17 Feb 2005 17:35:28 +0000
Subject: Re: traversing multi-dimensional arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-208934@lists.php.net to get a copy of this message
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")); Why do you even need the $cars array? Wouldn't this work?
foreach($models as $car => $model_array) { foreach($model_array as $key => $model) {
    print "The car is a $car $model";
} } If you know what manufacturer you want ('ford' or 'chevy') then you can just get the models for that by doing this: $car = 'ford'; foreach($models[$car] as $key => $model) { print "The car is a $car $model"; } - Jamie
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){
                        }
} Thanks!


« previous php.general (#208934) next »