开发者

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! 
) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜