Altering the elements of a collection in a foreach loop
Can I do the following in PHP?
foreach ($c开发者_JAVA百科ollection as &$element)
$element = ...
Almost - you should remove the %
. Here's an example, which worked locally with PHP 5.3.4 :
$foo = array("1" => "First", "2" => "Second");
foreach($foo as $key => & $element) {
$element = $element . " With More Text Attached!\n";
}
print_r($foo);
results in
Array (
[1] => First With More Text Attached!
[2] => Second With More Text Attached!
)
精彩评论