Re: while question

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

 



Ashley Sheridan wrote:
On Mon, 2008-11-17 at 18:01 -0500, Craige Leeder wrote:
Ashley Sheridan wrote:
On Mon, 2008-11-17 at 17:47 -0500, Craige Leeder wrote:
Only thing to note with the foreach is that you are actually working on a copy of the array, so if you intend to modify it, pass it by reference.

- Craige
Can you do that? I assume it would look like this:

foreach(&$array as $a) {}
Close. It actually looks like this:

foreach ($array as $key => &$value)  {}

This makes sense because for each iteration of the loop, PHP places a copy of the key in $key, and a copy of the value in $value. Therefor, you are specifying with this code that you want it to place a reference of the value into $value.

- Craige



Ah, that could be very useful to know, thanks!


Ash
www.ashleysheridan.co.uk


foreach ($array as $key => $value)

$value = a copy so won't change the original array
$array[$key] = the original

thus:

foreach ($array as $key => $value) {
 $array[$key] = do_something_with($value);
}

--
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