Home > php教程 > php手册 > 探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释

探讨:php中在foreach中使用foreach ($arr as &$value) 这种类型的解释

WBOY
Release: 2016-06-13 11:45:55
Original
874 people have browsed it

自 PHP 5 起,可以很容易地通过在 $value 之前加上 & 来修改数组的元素。此方法将以引用赋值而不是拷贝一个值。

复制代码 代码如下:


$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>


此方法仅在被遍历的数组可以被引用时才可用(例如是个变量)。

复制代码 代码如下:


foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}
?>

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template