2011-03-22 82 views
0

我可以在PHP中執行以下操作嗎?在foreach循環中更改集合的元素

foreach ($collection as &$element) 
    $element = ... 
+0

嗯,[你看了手冊](http://php.net/manual /en/control-structures.foreach.php)? – lonesomeday 2011-03-22 23:20:58

+0

是的。雖然我假設你打算輸入'$'而不是'%',那麼 – 2011-03-22 23:21:58

+0

@Sam:是的,我打算輸入'$'。我仍然不習慣PC鍵盤(和一般的PC)。 – pyon 2011-03-22 23:43:12

回答

1

幾乎 - 你應該刪除%。下面是一個例子,它與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); 

結果

Array ( 
    [1] => First With More Text Attached! 
    [2] => Second With More Text Attached! 
) 
+0

對不起,'%'實際上是一個錯字。非常感謝你! – pyon 2011-03-22 23:44:27