Page 1 of 1

weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 4:18 am
by jmut
Basically a snippet to get last itteration and do something based on that.
So my question is why it acts like that. I would expect to have only "This is last 7"

Code: Select all

$array = array(1,2,3,4,5,6,7);
foreach ($array as $i => $a) {
    if (!next($array))  {
        echo "This is last {$a}" ."\n";
        continue;
    }
 
    echo "$a\n";
}
//output
/*
1
2
3
4
5
This is last 6
This is last 7
*/
 

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 4:38 am
by Ziq
I'm not sure, but I think problem in foreach. Why do you use a foreach loop?

Code: Select all

 
<?
$array = array(1,2,3,4,5,6,7);
 
while ( true ) 
{
    $data = current($array);
    
    if (!next($array))
    {
        echo 'This is last '.$data."\r\n";
        break;
    }
    
    
    echo $data."\r\n";
}
/*OUTPUT:
1
2
3
4
5
6
This is last 7
 */
?>
 

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 4:57 am
by requinix
Because foreach does some stuff with the array's internal pointers.
next() returns the next thing, or false if there isn't one. The first call to next() in the loop would logically return 2, however foreach does that once already - so the first next() actually returns 3.
Meaning each call will be off by one relative to what you would expect.

Code: Select all

$array = array(1, 2, 3, 4, 5);
echo next($array); // 2
echo next($array); // 3
echo next($array); // 4
 
foreach ($array as $i) { // reset pointer and advance by one
    echo next($array); // 3 4 5 (nothing) (nothing)
}
Might be a bug.

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 5:05 am
by Kadanis
What version of PHP are you running, I know this isn't helpful but I just ran your original code on our test box and it worked perfectly

Output
1
2
3
4
5
6
This is last 7

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 5:14 am
by pcoder
same here.. :)

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 5:22 am
by Ziq
Strangely... I have same results with Jmut. PHP Version is 5.2.4

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 5:26 am
by pcoder
I am using PHP Version 5.1.2 and getting the output as:

Code: Select all

 
1 2 3 4 5 6 This is last 7
 

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 5:36 am
by Kadanis
Looks like something may have changed in PHP.

On a 5.1.6 test machine it worked as expected, on a 5.2.6 test machine it produced the errored results... weird.

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 6:55 am
by dude81
report a bug to PHP

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 8:38 am
by jmut
damn. php is flawed as seems. well I am using php 5.2.6
Will look into bug spool and fire one. Pretty weird.

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 9:00 am
by jmut
http://bugs.php.net/bug.php?id=46279&edit=2
Issue seems reported already. Totally frustrated with stupid answer. I'd say inconsistancy will kill php..nothing else can.

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 9:08 am
by dude81
Bugs are there everywhere.. in Java, C# on almost all languages or for that matter even in our own code. "To Err is Human " :wink:

Re: weird Iterration issue. foreach with !next($arr)

Posted: Wed Oct 15, 2008 12:47 pm
by jmut
dude81 wrote:Bugs are there everywhere.. in Java, C# on almost all languages or for that matter even in our own code. "To Err is Human " :wink:
I don't mind bugs at all...they are inevitable. What I am frustrated with is unconsistancy. Cause when you examine changelog you'll see no such change and you say ok my code will work...and all of sudden it goes crazy. Just wait to see who will burst in yell to tell me something doesn't work now.