SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: foreach

  1. #1
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach

    Hey,
    I was wondering if anybody new the differences between foreach and for. WHen strictly of speaking of iterating through arrays. I know in a for loop you can set the starting index/key. So my real question I guess is 'When iterating through an entire array are the differences between for loop and a foreach loop purely cosmetic?' I personally use for loops out of habit from c/++ but am trying to use more foreach loops.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use a for loop for an array you cannot address the key. Im guessing your doing something like:

    PHP Code:
    for($i=0;$i<count($array);$i++){
       echo 
    $array[$i];

    But say your array has text in the key like:
    $array['apple'] = A spherical fruit with a stem.
    $array['car'] = A small automobile with 4 wheels

    You would want to do a foreach so you can address a key that ins't numeric.
    PHP Code:
    foreach($array as $key=>$value){
      echo 
    "A $key is a $value";

    Hope this makes sense. I personally always use foreach when dealing with an array.

    e39m5

  3. #3
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, made perfect since. But other than that they're both as efficient?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you move the count($array) out of the condition so that it doesn't need to be evaluated on every iteration, then they're pretty much equivalent.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    If you move the count($array) out of the condition so that it doesn't need to be evaluated on every iteration, then they're pretty much equivalent.
    How does foreach iterate, well not so much iterate as much as determine length of array.
    I'm still just confused on them, seem to be identical. Minus the function call

    foreach($array as $key=>$value){
    echo "A $key is a $value";
    }

    In the above example it iterates the same way, except that you can tie the array index to a string.

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    By looking at the array in memory. Same thing count() does. What's confusing? That two functions can serve similar purposes?

    PHP Code:
    //Iterate over an array

    //
    foreach ($array as $val) {
      echo 
    $val;
    }

    //
    foreach ($array as $key => $val) {
      echo 
    $key " = " $val;
    }

    //
    for ($i 0$i count($array); $i++) {
      echo 
    $i " = " $array[$i];
    }

    //
    $i 0;
    do {
      echo 
    $i " = " $array[$i];
      
    $i++;
    } while (
    $i count($array);

    //
    $i 0;
    while (
    $i count($array)) {
      echo 
    $i " = " $array[$i];
      
    $i++;
    }

    //
    $size count($array);
    for (
    $i 0$i $size$i++) {
      echo 
    $i " = " $array[$i];
    }

    //
    for ($i count($array) - 1$i >= 0$i--) {
      echo 
    $i " = " $array[$i];
    }

    //
    function printone($n) {
      echo 
    $n;
    }
    array_map("printone"$array);

    //
    function printone($item$key) {
      echo 
    $key " = " $item;
    }
    array_walk($array"printone"); 

  7. #7
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You also missed
    Code PHP:
    while (list($key, $val) = each($fruit)) {
        echo "$key => $val\n";
    }

    I was asking where foreach looked at array length, because if you say the main differences is when most people iterate through an array they call count function. Then you implied that foreach only gets checks memory for array length once. I was just wondering where it did this in the scheme of things but it doesn't really matter.

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Isn't foreach() working on a copy?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •