> -----Original Message----- > From: Gary [mailto:php-general@xxxxxxxxxxxxxxx] > Sent: Tuesday, October 19, 2010 11:38 PM > To: php-general@xxxxxxxxxxxxx > Subject: Re: Possible foreach bug; seeking advice to isolate the > problem > > Jonathan Sachs wrote: > > I've got a script which originally contained the following piece of > > code: > > > > foreach ( $objs as $obj ) { > > do_some_stuff($obj); > > } > > > > When I tested it, I found that on every iteration of the loop the last > > element of $objs was assigned the value of the current element. > > I only had a few minutes to look at it, but here is my 2 Euro cents. > > It only occurs when you have previously done > ,---- > | foreach ( $objs as &$obj ) { > | //anything or nothing here > | } > `---- > as described in the documentation. > > > That leaves me with the question: what is going wrong with foreach? > > I'm trying to either demonstrate that it's my error, not the PHP > > engine's, or isolate the problem in a small script that I can submit > > with a bug report. > > From the post you reference in the manual's comments: > $a = array('a', 'b','c'); > foreach($a as &$row){ > //you don't have to do anything here > } > print_r($a); > foreach($a as $row){ > echo "<br />".$row; > } > print_r($a); > > This suffices. > > > I tried to eliminate the database by doing a var_export of the array > > after I built it, then assigning the exported expression to a variable > > immediately before the foreach. That "broke the bug" -- the loop > > behaved correctly. > > Yup. I guess whatever database code used previously was doing the > equivalent of the first foreach in the previous code snippet. > > > Can anyone make suggestions on this -- either insights into what's > > wrong, or suggestions for producing a portable, reproducible example? > > Better. I can tell you how to solve it: > $a = array('a', 'b','c'); > foreach($a as &$row){ > //you don't have to do anything here > } > unset($row); // <----<<< THIS IS KEY! Shouldn't that be $row = null since unset will remove the last value, not just removing the variable also, from the array whereas the $row = null will tell the reference pointer that it doesn't point to a value. > print_r($a); > foreach($a as $row){ > echo "<br />".$row; > } > print_r($a); > > I admit though, it's not obvious, even from reading the manual, and is > definitely a bug in the sense it is unexpected behaviour. Documenting it > and calling it a "feature" is appalling. > Regards, Tommy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php