开发者

Altering the elements of a collection in a foreach loop

开发者 https://www.devze.com 2023-02-18 07:39 出处:网络
Can I do the following in PHP? foreach ($c开发者_JAVA百科ollection as &$element) $element = ...

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

精彩评论

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