Looping through arrays with foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sjoerd

    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_expressi on as $value)
    statement
    foreach (array_expressi on 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.

  • Oli Filth

    #2
    Re: Looping through arrays with foreach

    Sjoerd said the following on 31/03/2006 11:29:[color=blue]
    > 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.[/color]

    Actually, no it wouldn't. count($arr) == 1, so it attempts to access
    only $arr[0], which clearly doesn't exist.




    --
    Oli

    Comment

    • Chung Leong

      #3
      Re: Looping through arrays with foreach

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

      The main problem with the list() = each() construct is that it has a
      side-effect--namely that of advancing the array's internal pointer.
      Forgetting to call reset() on an array was a common mistake in PHP 3.

      Comment

      • Richard Levasseur

        #4
        Re: Looping through arrays with foreach

        It should be said that foreach operates on a copy of the array.
        Changes to $value won't will not affect the original array.

        With PHP5, the referential foreach was added, which aliases $value to
        the original value in the array.
        foreach($array as $key => & $value) { ... }
        It should also be noted that $value will still exist as a reference
        after the foreach() is over, thus assignments to $value will affect the
        array also, so you much unset() it to prevent undesired side effects.

        More information is available in the manual at:


        Comment

        • chernyshevsky@hotmail.com

          #5
          Re: Looping through arrays with foreach

          Sjoerd wrote:[color=blue]
          > Another method to loop through an array is the following:
          > ?php
          > while (list(, $value) = each($arr)) {
          > echo "Value: $value<br />\n";
          > }
          > ?
          >[/color]
          The main problem with the list() = each() construct is that it has a
          side-effect--namely that of advancing the array's internal pointer.
          Forgetting to call reset() on an array was a common mistake in PHP 3.

          Comment

          Working...