#32959 [Csd->Bgs]: key() returns an incorrect key after a foreach()

From: Date: Fri, 06 May 2005 08:57:44 +0000
Subject: #32959 [Csd->Bgs]: key() returns an incorrect key after a foreach()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-78695@lists.php.net to get a copy of this message
ID: 32959 Updated by: tony2001@php.net Reported By: ben at bravenet dot com -Status: Closed +Status: Bogus Bug Type: Arrays related Operating System: Ubuntu Hoary 5.04 PHP Version: 4.3.10 Previous Comments: ------------------------------------------------------------------------ [2005-05-05 22:07:13] ben at bravenet dot com nm - just needed to read the manpage more carefully ------------------------------------------------------------------------ [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

« previous php.bugs (#78695) next »