Jump to content

Recommended Posts

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 . . . 
}

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. :)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.