php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35063 key() returns wrong value in function with array passed by reference
Submitted: 2005-11-02 10:20 UTC Modified: 2005-11-02 18:58 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: dave dot newman at codegate dot co dot uk Assigned: ilia
Status: Closed Package: Arrays related
PHP Version: 4CVS-2005-11-02 (CVS) OS: Windows XP
Private report: No CVE-ID:
 [2005-11-02 10:20 UTC] dave dot newman at codegate dot co dot uk
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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-02 14:21 UTC] phpdev at citromail dot hu
I myself also noticed this bug when I installed the news 4.4.1 release this morning. 4.4.0 worked fine, but 4.4.1 is broken. This bug should be fixed ASAP as this affects (breaks) primary functions of PHP and there's no workaround for it.
 [2005-11-02 18:58 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 09:02:23 2014 UTC