php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31181 foreach behaviour change in 4.3.10
Submitted: 2004-12-18 16:02 UTC Modified: 2004-12-18 18:19 UTC
From: shustrik at softech dot lv Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.10 OS: Linux 2.4.20
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: shustrik at softech dot lv
New email:
PHP Version: OS:

 

 [2004-12-18 16:02 UTC] shustrik at softech dot lv
Description:
------------
Apparently, the foreach loop such as "foreach ($array as $value)" puts some kind of index-value array into $value. This differs from what the documentation says and does not seem to appear in changelog or make any sense. The code below produces different results on 4.3.9 and 4.3.10

Reproduce code:
---------------
<?php
  $a = array(
    'ab' => array(
      'a' => 1,
      'b' => 2
    ),
    'cd' => array(
      'c' => 3,
      'd' => 4
    )
  );
  foreach ($a as $key => $array) {
    echo '<br />key ' . $key . ', array: ';
    var_dump($array);
    foreach ($array as $datafield) {
      echo '<br />key ' . $key . ', subarray element: ';
      var_dump($datafield);
    }
  }
?>

Expected result:
----------------
PHP 4.3.9: 

key ab, array: Array ( [a] => 1 [b] => 2 ) 
key ab, subarray element: 1 
key ab, subarray element: 2 
key cd, array: Array ( [c] => 3 [d] => 4 ) 
key cd, subarray element: 3 
key cd, subarray element: 4 

Actual result:
--------------
PHP 4.3.10: 

key ab, array: Array ( [a] => 1 [b] => 2 ) 
key ab, subarray element: Array ( [0] => 1 [1] => a ) 
key ab, subarray element: Array ( [0] => 2 [1] => b ) 
key cd, array: Array ( [c] => 3 [d] => 4 ) 
key cd, subarray element: Array ( [0] => 3 [1] => c ) 
key cd, subarray element: Array ( [0] => 4 [1] => d ) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-18 18:19 UTC] tony2001@php.net
Do not file bugs when you have Zend extensions (zend_extension=)
loaded. Examples are Zend Optimizer, Zend Debugger, Turck MM Cache,
APC, Xdebug and ionCube loader.  These extensions often modify engine
behavior which is not related to PHP itself.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 16:01:31 2024 UTC