Pikachu2000 Posted June 16, 2010 Share Posted June 16, 2010 Just want to see if there is a better way than I have been using to do this. I have a multidimensional array that can contain any number of elements, each of those elements contain 41 elements. This array comes from an external source, over which I have no control. I have been doing (essentially) the same as below. Is there a better/more efficient way to loop through the elements, or is this about as good as it gets? $num = count($clients); for( $i = 0; $i < $num; $i++) { print_r($clients[$i]); // this would be replaced and formatted in real life . . . } Quote Link to comment https://forums.phpfreaks.com/topic/204916-multidimensional-arrays-looping-through-them/ Share on other sites More sharing options...
Psycho Posted June 16, 2010 Share Posted June 16, 2010 foreach($clients as $client) { print_r($client); } Quote Link to comment https://forums.phpfreaks.com/topic/204916-multidimensional-arrays-looping-through-them/#findComment-1072797 Share on other sites More sharing options...
Pikachu2000 Posted June 16, 2010 Author Share Posted June 16, 2010 mjdamato, thanks for having a look. At the time I wrote the script, there was a reason I didn't use a foreach() loop, but I'll be damned if I can remember what it was. I guess that means either a) I'm too old, or b) there isn't a reason not to use it anymore. I'm leaning towards b. Quote Link to comment https://forums.phpfreaks.com/topic/204916-multidimensional-arrays-looping-through-them/#findComment-1072916 Share on other sites More sharing options...
Psycho Posted June 16, 2010 Share Posted June 16, 2010 Yeah, I can't think of any good reasons for the previous for loop. At first I thought it would make sense if there were indexes that were non-numerical that you didn't want to process. but, if that was the case, then you wouldn't be using the count() as the total to iterrate through. The only other possibility is that you wanted a variable to use to use as a counter to possibly display a number next to each item. But, again, using foreach() would be a better option with using a variable that you increment on each loop. Quote Link to comment https://forums.phpfreaks.com/topic/204916-multidimensional-arrays-looping-through-them/#findComment-1072930 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.