#23624 [Opn->Ver]: foreach internal array pointer issue

From: Date: Wed, 14 May 2003 14:59:18 +0000
Subject: #23624 [Opn->Ver]: foreach internal array pointer issue
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-39629@lists.php.net to get a copy of this message
 ID:               23624
 Updated by:       sniper@php.net
 Reported By:      nicolas at van-lancker dot be
-Status:           Open
+Status:           Verified
-Bug Type:         Arrays related
+Bug Type:         Scripting Engine problem
 Operating System: Windows 2000
-PHP Version:      4.3.1
+PHP Version:      4.3.2-RC3-dev


Previous Comments:
------------------------------------------------------------------------

[2003-05-14 03:50:22] 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...




------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=23624&edit=1



Thread (6 messages)

« previous php.bugs (#39629) next »