| 
        php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
  [2004-12-16 10:44 UTC] clemens at gutweiler dot net
 Description:
------------
foreach modifies an array, if key and value are the same variable.
see reproduction code.
Reproduce code:
---------------
<?php
$foo = array( 'foo' => 'bar', 'dings' => 'dongs' );
foreach( $foo as $k => $k ) {}
var_dump( $foo );
?>
array(2) {
  ["foo"]=>
  string(5) "dings"
  ["dings"]=>
  NULL
}
Expected result:
----------------
array(2) {
  ["foo"]=>
  string(3) "bar"
  ["dings"]=>
  string(5) "dongs"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             
             | 
    |||||||||||||||||||||||||||||||||||||
            
                 
                Copyright © 2001-2025 The PHP GroupAll rights reserved.  | 
        Last updated: Tue Nov 04 06:00:01 2025 UTC | 
I stumbled across another werd behavior related to this bug: [php] $var = array( "a" => array("foo"), "b" => array("fox"), "c" => "x" ); foreach($var as $key => $val) { echo "no matter what happends".$val; } /* $var is array( "a" => array("foo"), "b" => array("fox"), "c" => "foo" ); */ [/php]