Posted by Alan Little on 05/30/06 20:19
Carved in mystic runes upon the very living rock, the last words of David
Haynes of comp.lang.php make plain:
> iavian wrote:
>> hi
>>
>> i want to modify an array in foreach loop
>>
>> foreach($array as $ele){
>> $ele = $some_random_string;
>> }
>>
>> i should see here the modified $array .
>>
>> thnaks in advance
>>
> foreach( $array as &$ele ) {
> $ele = $some_random_string;
> }
>
> should work as you want it to.
Only in PHP5.
http://us3.php.net/manual/en/control-structures.foreach.php
"Unless the array is referenced, foreach operates on a copy of the
specified array and not the array itself....As of PHP 5, you can easily
modify array's elements by preceding $value with &. This will assign
reference instead of copying the value."
--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
[Back to original message]
|