Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post
    I've encountered some behavior in PHP (v5.2.4) that I don't understand.

    My script has several foreach loops that iterate over an array whose elements are arrays. The code looks like this:

    Code:
    foreach ( $objs as $obj ) {
     do_some_stuff($obj);
    }
    The first hint of trouble came when I ran this script directly in a browser and observed do_some_stuff() processing the next-to-last array element twice, and skipping the last element.

    In the debugger, I saw that each time the foreach loop iterated, the last element of the array was set equal to the current element. Thus, on the first iteration the last element was set equal to the first element, on the second iteration it was set equal to the second element... On the next-to-last iteration it was set equal to the next-to-last element, and on the last iteration it was set equal to itself. Since it contained the value of the next-to-last element at that point, that's the value it had when it was processed.

    I'm baffled by this. Is it some weird side effect of foreach? Is it a known bug?

    I'll point out a couple of things it's not.

    It's not a side effect of using a value by reference. The foreach statement says "$objs as $obj" -- not "$objs as &$obj."

    It's not a side effect of the function call, either. In one experiment I replaced the function call with a couple of echo statements, and the loop behaved exactly the same way.

    Also, when I replaced the code above with the following, it worked correctly:

    Code:
    for ( $i=0; $i<count($objs); ++$i ) {
     $obj = $objs[$i];
     do_some_stuff($obj);
    }
    That's my workaround until I find an explanation for what's happening.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Someone posted a lengthy discussion about this same or a similar behavior on php.net: PHP: foreach - Manual - comment 92116. He seems to be saying that if you have one foreach loop that accesses elements by reference, then a subsequent loop accessing the elements by value with the same variable name will run into the problem you're describing. He offers several workarounds. Does that seem relevant?

  3. #3
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post
    That comment does appear to be the same problem.

    More weirdness: I read it when I was trying to figure out what was going on, but until you fingered it, it didn't seem the same to me.

    Thanks for digging this out. It doesn't solve the problem, but it does make the problem make sense!

Posting Permissions

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