Edit report at http://bugs.php.net/bug.php?id=51409&edit=1
ID: 51409 Updated by: ras...@php.net Reported by: sramage at nucleuslabs dot com Summary: foreach structure pass by reference scope issue -Status: Open +Status: Bogus Type: Bug Package: Arrays related Operating System: FREEBSD 8.0 PHP Version: 5.2.13 New Comment: This makes perfect sense. After your first foreach loop, $banana is a reference to the last element in your $bananas array. Anything you do to that $banana variable is going to affect that last element. So, in your coconut foreach, you are now asking PHP to assign the first element of the coconut array to the last element of your bananas array which is exactly the output you are getting. No bug here. Previous Comments: ------------------------------------------------------------------------ [2010-03-26 22:31:41] sramage at nucleuslabs dot com 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! ) ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51409&edit=1