Re: Looping through arrays with foreach
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: Looping through arrays with foreach

From: Richard Levasseur <richardlev@gmail.com>
Date: Fri Mar 31 2006 - 18:22:38 CEST

It should be said that foreach operates on a copy of the array.
Changes to $value won't will not affect the original array.

With PHP5, the referential foreach was added, which aliases $value to
the original value in the array.
foreach($array as $key => & $value) { ... }
It should also be noted that $value will still exist as a reference
after the foreach() is over, thus assignments to $value will affect the
array also, so you much unset() it to prevent undesired side effects.

More information is available in the manual at:
http://us2.php.net/manual/en/control-structures.foreach.php
Received on Mon May 1 02:45:53 2006