php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32959 key() returns an incorrect key after a foreach()
Submitted: 2005-05-05 21:11 UTC Modified: 2005-05-06 10:57 UTC
From: ben at bravenet dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.3.10 OS: Ubuntu Hoary 5.04
Private report: No CVE-ID: None
 [2005-05-05 21:11 UTC] 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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-05 22:07 UTC] ben at bravenet dot com
nm - just needed to read the manpage more carefully
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 16:01:34 2024 UTC