php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23624 foreach internal array pointer issue
Submitted: 2003-05-14 03:50 UTC Modified: 2003-06-16 05:32 UTC
From: nicolas at van-lancker dot be Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.2-RC3-dev OS: any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nicolas at van-lancker dot be
New email:
PHP Version: OS:

 

 [2003-05-14 03:50 UTC] nicolas at van-lancker dot be
According to the documentation :

Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified as with the each() construct and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array.

Given the code :

<?php
	$arr = array("one", "two", "three");

	echo "current : " . current($arr) . "<br>";

	foreach ($arr as $key => $value) {
    		echo "Key: $key; Value: $value<br>";
	}
	
	echo "after for each : " . current($arr) . "<br>";
?>

Code result :

current : one
Key: 0; Value: one
Key: 1; Value: two
Key: 2; Value: three
after for each : 

It seems that the internal array pointer indeed has been moved while iterating. But I would expect that it should be a the end of the original array, returning me a value "three" instead of nothing. When calling prev($arr); it still doesn't return anything. Maybe the interal array pointer has gone beserk?

This might be a wrong reflexion by me and not be a bug. But it find it a bit confusing...



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-14 10:28 UTC] philip@php.net
Here's the original report on this [related] issue:
http://bugs.php.net/bug.php?id=8353

foreach affects the pointer and moves it "past" the end.  I assume that if your "after the each" printed "three" that we would have an endless loop printing "three" infinity times...

For the record I don't think foreach() should touch the pointer at all but it does so this simply looks like a documentation problem.  Rather than say "end" it should say something like "moves past the end".  Hopefully a php-dev person can provide proper terminology for this behavior in which case it will be documented.
 [2003-05-14 10:36 UTC] sniper@php.net
This is more common problem:

<?php

$arr = array("one", "two", "three");

var_dump(current($arr));
foreach ($arr as $v);
var_dump(current($arr));

reset($arr);
var_dump(current($arr));
while(each($arr));    
var_dump(current($arr));

?>

And output is:

string(3) "one"
bool(false)
string(3) "one"
bool(false)

So, as you can see, it's not just foreach(), each() also
does the same, ie. sets current key to null.
IMO, current documentation is correct about it.
(the key should be set to the last key)

The question is if it should be fixed, as it's propably been around there since the beginning..but I leave that decision to Zeev / Andi.

For the moment, leave this as 'Verified'.


 [2003-05-14 10:39 UTC] sniper@php.net
Minor correction: Maybe the bug is actually in key/next/current which should not return false/null but the last element in these cases.. :)

 [2003-06-16 05:32 UTC] stas@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I see no reason whatsoever to make it point to the last array element - like, what would be the use of it? When the loop ends, current() and his friends (last, each, etc.) should return false - this is exactly the sign that you've reached the end of the array. If you want another loop, do reset. 

I also think you misunderstand the docs - 'pointer is at the end of the array' is not the same as 'pointer is at the last element of the array' but the same as 'there's no more elements in the array, so you must do reset before using any array pointer functions'. 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Aug 29 07:00:02 2025 UTC