|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-03-26 22:36 UTC] rasmus@php.net
-Status: Open
+Status: Bogus
[2010-03-26 22:36 UTC] rasmus@php.net
[2010-03-27 01:03 UTC] sramage at nucleuslabs dot com
[2010-03-27 01:43 UTC] rasmus@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 30 16:00:01 2025 UTC |
Description: ------------ When an ampersand operator is used in a foreach language construct The pass by reference variable causes *copies* of the parent array used in the foreach loop to be modified in any future use of the initial pass by reference variable. This is very hard to explain clearly but see the test script below for clarification it is a very simple test script.' Similar bugs like this have been reported and documentation outlines a simple workaround that does work and solves my problem. but I figured I would report this problem as *copies* of the array are affected and it seems kind of strange. Test script: --------------- class Monkey{ public $bananas=array(); public function AddBananas($bananas){ $this->bananas=$bananas; }} $bananas=array(); $bananas['banana1']=array('color'=>'yellow','size'=>'big'); $bananas['banana2']=array('color'=>'green','size'=>'small'); $coconuts=array(); $coconuts['coconut1']['size']='tiny'; $coconuts['coconut2']['size']="I'm a"; $monkey=new Monkey(); foreach($bananas as $key=>&$banana){ $banana['id']=$key+1; } $monkey->AddBananas($bananas); foreach($coconuts as $banana){ $banana['type']="coconut!"; } print_r($monkey->bananas); Expected result: ---------------- Array ( [banana1] => Array ( [color] => yellow [size] => big [id] => 1 ) [banana2] => Array ( [color] => green [size] => small [id] => 2 ) ) Actual result: -------------- Array ( [banana1] => Array ( [color] => yellow [size] => big [id] => 1 ) [banana2] => Array ( [size] => I'm a [type] => coconut! ) )