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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
22 - 10 = ?
Subscribe to this entry?

 
 [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 18:01:33 2024 UTC