#8353 [Csd->Opn]: foreach modify array pointer
| From: | philip@php.net | Date: | Thu, 21 Nov 2002 21:21:34 +0000 |
| Subject: | #8353 [Csd->Opn]: foreach modify array pointer | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-25446@lists.php.net to get a copy of this message | ||
ID: 8353
Updated by: philip@php.net
Reported By: lunepi@comlink.org
-Status: Closed
+Status: Open
Bug Type: Arrays related
-PHP Version: 4.0.4
+PHP Version: 4.3.0-dev
New Comment:
foreach essentially REMOVES the array pointer alltogether on the
original array.
$arr = array('a','b'); foreach ($arr as $v); var_dump( current($arr)
);
$arr = array('a','b'); foreach ($arr as $v); reset($arr); var_dump(
current($arr) );
Results:
bool(false)
string(1) "a"
Using key() instead of current() results in NULL instead of false.
Where did the pointer go?
Conclusion:
Either foreach() has a feature that removes the array pointer from the
original array or it's a bug. If it's seen as a feature please explain
why so it can be documented.
Previous Comments:
------------------------------------------------------------------------
[2001-03-09 21:32:57] sniper@php.net
no feedback.
------------------------------------------------------------------------
[2001-01-05 22:52:10] sniper@php.net
I get 121 for both cases. Please try the latest snapshot from
http://snaps.php.net/
--Jani
------------------------------------------------------------------------
[2000-12-21 09:44:03] lunepi@comlink.org
try 1.:
$var[] = "1";
$var[] = "2";
foreach($var as $test)
{
echo $test;
}
reset($var);
echo current($var);
output -> 12
then try:
$var[] = "1";
$var[] = "2";
//reset($var);
foreach($var as $test)
{
echo $test;
current($var);
}
echo current($var);
output -> 121
this should be the output of the first script too?
Greetings lunepi
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=8353&edit=1