Re: traverse a multidimensional array

From: Date: Fri, 21 Jul 2000 09:45:07 +0000
Subject: Re: traverse a multidimensional array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-7535@lists.php.net to get a copy of this message
I came up the code below this morning - which I think does what I want (server didn't fall over anyway:)) - i.e recurses through through a deep structure and lists every element. function get_array_elems($arrResult){ while(list($key,$value)=each($arrResult)){ if (is_array($value)){ get_array_elems($value); } else { for ($i=0; $i<count($value);$i++){ echo "array[$key]=$value<br>\n"; } } } } get_array_elems($arrResult); Thanks for the help Lez ----- Original Message ----- > > It occurred to me though, is there a way to recurse through > > the structure if the dimensions are not known? > > count() is a good way. If you are using that loop where some > of the values are arrays and some are not, you can just check > the type of $value and if it is an array, loop through that as > well. The best solution for something like this is a recursive > function. > > Chris ----- Original Message ----- From: "Boget, Chris" <Chris.Boget@wild.net> To: "'Lez Lytollis'" <lez.lytollis@ccr.ntu.ac.uk>; "PHP General" <php-general@lists.php.net> Sent: Thursday, July 20, 2000 3:28 PM Subject: RE: [PHP] traverse a multidimensional array > > OK, thanks, Chris, good of you to answer - a classic case of > > RTFM from me there! > > Been there. :) > > > It occurred to me though, is there a way to recurse through > > the structure if the dimensions are not known? > > count() is a good way. If you are using that loop where some > of the values are arrays and some are not, you can just check > the type of $value and if it is an array, loop through that as > well. The best solution for something like this is a recursive > function. > > Chris

« previous php.general (#7535) next »