#32959 [Opn->Csd]: key() returns an incorrect key after a foreach()
| From: | ben at bravenet dot com | Date: | Thu, 05 May 2005 20:07:14 +0000 |
| Subject: | #32959 [Opn->Csd]: key() returns an incorrect key after a foreach() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-78673@lists.php.net to get a copy of this message | ||
ID: 32959
User updated by: ben at bravenet dot com
Reported By: ben at bravenet dot com
-Status: Open
+Status: Closed
Bug Type: Arrays related
Operating System: Ubuntu Hoary 5.04
PHP Version: 4.3.10
New Comment:
nm - just needed to read the manpage more carefully
Previous Comments:
------------------------------------------------------------------------
[2005-05-05 21:11:59] ben at bravenet dot com
Description:
------------
After a foreach() on an array, key() won't return a value from the same
array.
It looks like an internal array pointer problem, since a reset() before
key() appears to fix it.
Reproduce code:
---------------
<?php
$foo = array('a'=>'1', 'b'=>'2',
'c'=>'3');
foreach($foo as $k=>$v)
{
echo "$k=>$v\n";
}
// reset($foo); // this fixes the problem
echo key($foo);
?>
Expected result:
----------------
a=>1
b=>2
c=>3
a
Actual result:
--------------
a=>1
b=>2
c=>3
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=32959&edit=1