#35063 [NEW]: key() returns wrong value in function with array passed by reference
| From: | dave dot newman at codegate dot co dot uk | Date: | Wed, 02 Nov 2005 09:20:41 +0000 |
| Subject: | #35063 [NEW]: key() returns wrong value in function with array passed by reference | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-87389@lists.php.net to get a copy of this message | ||
From: dave dot newman at codegate dot co dot uk
Operating system: Windows XP
PHP version: 4CVS-2005-11-02 (CVS)
PHP Bug Type: Arrays related
Bug description: key() returns wrong value in function with array passed by reference
Description:
------------
I call a function with a reference to an array and an object.
I then add the object to the end of the array.
When I then use end() and key() to get the key of the the object just
added to the array key() always returns 0 rather than the actual key
value.
Reproduce code:
---------------
function addToArray(&$array, $obj)
{
$array[] = $obj;
print_r($array);
end($array);
print 'last key = '.key($array).'<hr/>';
}
print '<pre>';
$array = array();
for ($a=0; $a<5; $a++)
addToArray($array, array($a=>$a));
print '</pre>';
Expected result:
----------------
I expect it to print to the screen an array dump followed by the last key
value in the array 5 times (seperated by <hr>).
I add an object to the array each time, so the last key value displayed
should go from 0 to 4.
Actual result:
--------------
the last key value is always 0 rather than the actual last key value.
--
Edit bug report at http://bugs.php.net/?id=35063&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=35063&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=35063&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=35063&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=35063&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=35063&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=35063&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=35063&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=35063&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=35063&r=support
Expected behavior: http://bugs.php.net/fix.php?id=35063&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=35063&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=35063&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=35063&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35063&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=35063&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=35063&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=35063&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=35063&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=35063&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=35063&r=mysqlcfg