php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35019 key() on array inside for loop returning wrong entry
Submitted: 2005-10-30 00:18 UTC Modified: 2005-10-30 19:04 UTC
From: pee at erkkila dot org Assigned: iliaa (profile)
Status: Closed Package: Arrays related
PHP Version: 5.1.0RC4 OS: linux
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: pee at erkkila dot org
New email:
PHP Version: OS:

 

 [2005-10-30 00:18 UTC] pee at erkkila dot org
Description:
------------
Found a difference in mediawiki 1.5 with php4 vs php5. After tracking it down it looks like key() might be returning incorrect information inside a for loop. I might be missing a scope change someplace too though. This test case is based on Parser.php from that package.

Reproduce code:
---------------
<?php
function one(&$state) {
    $state = array (
        'oneKey' => "oneValue",
        'twoKey' => "twoValue",
        'threeKey' => "threeValue",
        'fourKey' => "fourValue",
    );
}

function two(&$state) {
    for ( $contentDict = end( $state ); $contentDict !== false; $contentDict = prev( $state ) ) {
        print("Key:" . key($state) . "\n");
        print("Value:" . $contentDict . "\n");
    }
}
one($state);
two($state);
?>


Expected result:
----------------
Key:fourKey
Value:fourValue
Key:threeKey
Value:threeValue
Key:twoKey
Value:twoValue
Key:oneKey
Value:oneValue


Actual result:
--------------
Key:oneKey
Value:fourValue
Key:oneKey
Value:threeValue
Key:oneKey
Value:twoValue
Key:oneKey
Value:oneValue


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-30 19:04 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-2025 The PHP Group
All rights reserved.
Last updated: Mon Feb 03 06:01:30 2025 UTC