Array iteration
suggest changeSyntax
for ($i = 0; $i < count($array); $i++) { incremental_iteration(); }for ($i = count($array) - 1; $i >= 0; $i--) { reverse_iteration(); }foreach ($data as $datum) { }foreach ($data as $key => $datum) { }foreach ($data as &$datum) { }
Remarks
Comparison of methods to iterate an array
| Method | Advantage |
|---|---|
foreach |
The simplest way to iterate an array. |
foreach by reference |
A simple way to iterate and change elements of an array. |
for with an incremental index |
Iterates in any order, for example skipping elements or walking backward. |
| Internal array pointers | Steps through the array without a loop, so each function call, signal, and so on can advance once. |
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents