#23624 [NEW]: foreach internal array pointer issue

From: Date: Wed, 14 May 2003 08:50:22 +0000
Subject: #23624 [NEW]: foreach internal array pointer issue
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-39614@lists.php.net to get a copy of this message
From:             nicolas at van-lancker dot be
Operating system: Windows 2000
PHP version:      4.3.1
PHP Bug Type:     Arrays related
Bug description:  foreach internal array pointer issue

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



-- 
Edit bug report at http://bugs.php.net/?id=23624&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=23624&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=23624&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=23624&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=23624&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=23624&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=23624&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=23624&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=23624&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=23624&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=23624&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=23624&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=23624&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=23624&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=23624&r=gnused



Thread (6 messages)

« previous php.bugs (#39614) next »