Re: Possible foreach bug; seeking advice to isolate the problem
| From: | richard gray | Date: | Wed, 20 Oct 2010 06:19:08 +0000 |
| Subject: | Re: Possible foreach bug; seeking advice to isolate the problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-308876@lists.php.net to get a copy of this message | ||
On 20/10/2010 05:47, Jonathan Sachs wrote:
I've got a script which originally contained the following piece of code: foreach ( $objs as $obj ) {foreach ($objs as &$obj) { do_some_stuff($obj); } or is the above code a direct lift from your script? Referencing foreach statements can cause problems as the reference to the last array entry is persistent after the foreach loop has terminated so any further foreach statements on the same array will overwrite the previous reference which is still pointing to the last item. Richdo_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 was able to step through the loop and watch this happening, element by element. Are you are using a 'referencing' foreach? i.e.