RE: Possible foreach bug; seeking advice to isolate the problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> -----Original Message-----
> From: Jonathan Sachs [mailto:081012@xxxxxxxxxxx]
> Sent: 20 October 2010 04:48
> To: php-general@xxxxxxxxxxxxx
> Subject:  Possible foreach bug; seeking advice to isolate the
> problem
> 
> 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
> was
> able to step through the loop and watch this happening, element by
> element.

All the other suggestions I've seen on this are essentially correct -- before the foreach runs, something somewhere has set $obj to be a reference to the last element of the array.

 It really doesn't matter whether you can find the culprit for this or not -- the solution is simply to put an

  unset($obj)

immediately before the foreach statement -- this will break the reference (without destroying anything but $obj!) and make the foreach behave exactly as you want.

Cheers!

Mike

 -- 
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,  
Leeds Metropolitan University, C507 City Campus, 
Woodhouse Lane, LEEDS,  LS1 3HE,  United Kingdom 
Email: m.ford@xxxxxxxxxxxxxx 
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux