Home > Backend Development > PHP Tutorial > Discussion: This type of explanation of using foreach ($arr as &$value) in foreach in PHP_PHP Tutorial

Discussion: This type of explanation of using foreach ($arr as &$value) in foreach in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:03:57
Original
931 people have browsed it

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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327823.htmlTechArticleSince PHP 5, this can be easily done by prepending $value with
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template