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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post 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
*/
 
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

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

Post 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
 */
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

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

Post 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
Last edited by Kadanis on Wed Oct 15, 2008 5:14 am, edited 1 time in total.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

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

Post by pcoder »

same here.. :)
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

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

Post by Ziq »

Strangely... I have same results with Jmut. PHP Version is 5.2.4
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

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

Post 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
 
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

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

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

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

Post by dude81 »

report a bug to PHP
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

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

Post 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:
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post 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.
Post Reply