Since PHP 5, you can easily modify the elements of an array by preceding $value with &. This method assigns by reference rather than copying a value.
Copy code The code is as follows:
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>
This method is only available when the array being traversed can be referenced (for example, it is a variable).
Copy code The code is as follows:
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
http://www.bkjia.com/PHPjc/327823.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327823.htmlTechArticleSince PHP 5, this can be easily done by prepending $value with