循环数组更改元素类型

Agn*_*kas 3 php arrays pass-by-reference

考虑以下PHP代码片段,该片段按值引用在数组中循环:

$arr = [1 ,2 , 3];
var_dump($arr);
echo '<br>';

foreach ($arr as &$val) { // note ampersand sign
   // Anything
}

var_dump($arr);
Run Code Online (Sandbox Code Playgroud)

现在第一个var_dump()发出

array(3){[0] => int(1)[1] => int(2)[2] => int(3)}

但是第二个发出

array(3){[0] => int(1)[1] => int(2)[2] =>&int(3)}

因此,按引用在数组中循环已更改了最后一个值的类型-从整数更改为整数引用!怎么可能呢?为什么PHP在没有开发人员意图的情况下决定自行更改元素类型?

IMS*_*SoP 7

To understand this behaviour, you need to understand a few things about PHP:

  • References in PHP are symmetrical: you don't "create a reference to a variable", you "add the variable to a reference set".
  • A foreach by reference is the same as a series of assignments by reference, one after the other.
  • Array values are treated as variables in their own right, and can have all the same type information, including being part of a reference set.

So let's "unroll" your loop:

// create a reference set containing $val and $arr[0]
$val =& $arr[0];
// remove $val from the first reference set, 
// and create a second reference set containing $val and $arr[1]
$val =& $arr[1];
// remove $val from the second reference set, 
// and create a third reference set containing $val and $arr[2]
$val =& $arr[2];
Run Code Online (Sandbox Code Playgroud)

At this point, $arr[0] and $arr[1] are each in a reference set of size 1, so can be seen as "normal values". However, $arr[2] is still in a reference set with $val.

What that means is that any change to $arr[2] will be reflected in $val, and any change in $val will be reflected in $arr[2]. That's why var_dump is annotating that item with an &, to show that changing it will also change another variable somewhere else.

This is why it is a good habit to always run unset($val); after using a foreach-by-reference.