views:

597

answers:

2

which one is the fastest for iterating through arrays in php? or does another exist which is also faster for iterating through arrays?

+5  A: 

Even if there is any kind of difference, that difference will be so small it won't matter at all...

If you have, say, one query to the database, it'll take so long compared to the loop iterating over the results that the eternal debate of for vs foreach vs while will not change a thing -- at least if you have a reasonable amount of data.


So, use :

  • whatever you like
  • whatever fits your programming standard
  • whatever is best suited for your code / application

There will be plenty of other things you could / should optimize before thinking about that kind of micro-optimization ;-)


And if you really want some numbers (even if it's just for fun), you can either make some benchmark, or take a look at, for instance, the results of this one.

Pascal MARTIN
What the..?^^ I did not know that a winserver with ASP is that faster than a unix system...
daemonfire300
+1  A: 

Make a benchmark test.

There is no major "performance" difference, because the differences are located inside the logic.

  • You use foreach for array iteration, without integers as keys.
  • You use for for array iteration with integers as keys.
  • etc.
daemonfire300