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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ben at bravenet dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC