Looping through arrays with foreach



Summary: Use foreach(..) instead of while(list(..)=each(..)).

--=[ How to use foreach ]=--

Foreach is a language construct, meant for looping through arrays.
There are two syntaxes; the second is a minor but useful extension
of the first:

foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement

For example:
<?php
$arr = array("one", "two", "three");
foreach ($arr as $value) {
echo "Value: $value\n";
}
?>

This will output:
Value: one
Value: two
Value: three


--=[ Why not to use list & each ]=--

Another method to loop through an array is the following:
<?php
while (list(, $value) = each($arr)) {
echo "Value: $value<br />\n";
}
?>

This has some disadvantages:
- It is less clear, because list() looks like a function but
actually works the other way around: instead of returning
something, it takes a parameter by being the left-hand side
of the expression and assigns values to the parameters.
- You have to reset() the array if you want to loop through it
again.
- It is approx. three times slower than foreach.


--=[ About for & count ]=--

Another method to loop through an array:
for ($i = 0; $i < count($arr); $i++) {
echo "value: $arr[$i]\n";
}

Consider this array:
array(5 => "Five");

The above for-loop would loop from elements 0 through 5, while
only one element exists. If this is what you want, the for-loop
is an effective way to loop through an array. If not, use
foreach instead.

.



Relevant Pages

  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: foreach statement output
    ... know about foreach loop workings. ... I've got an incoming array of unknown ... The Perl Cookbook ... My big confusion over this foreach issue is I failed to realize that the ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Strange behaviour
    ... Personally, I would use the foreach version, as it is easier to read. ... bool AllNegative{ ... besides the difference between for and foreach the first example will always test each element of the array while the second example only tests until it finds the first non-negative. ... This is independant of for/foreach since also a for loop can be exited with return ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PHP] foreach() using current() strange beahvior
    ... that's expected as foreach moves the internal array pointer, ... When using the internal pointer just by calling current(so not moving ... Unless the array is referenced, foreach operates on a copy of the ...
    (php.general)
  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)