Reference problem of php circular array, zval structure

$arr=array("a","b","c","d");

foreach ($arr as &$value) {

}
//$value=&arr[3];$value="d";

foreach ($arr as $key=>$value) {
  echo "
";
  var_dump($arr);

}

the results are as follows

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  &string(1) "a"
}

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  &string(1) "b"
}

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  &string(1) "c"
}

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  &string(1) "c"
}

I would like to ask that $value=&arr [3] has been determined before the second foreache function; then the following
$value=$arr [0] in the first loop; whether it completely covers the first $value,
and why, and the last value is whether abcc, can be explained from the zval structure

Mar.12,2021

your understanding is correct, think about it:)

after the first foreach, $value points to the fourth element of the array:

[a b c d]
       ^
       value

the second foreach, overrides $value each time, and the first three times are like this:

[a b c a]
 ^     ^
       value

[a b c b]
   ^   ^
       value

[a b c c]
     ^ ^
       value

cycle to the fourth element for the fourth time, because it has been overwritten as c at the third time, so the fourth time is also c

.
Menu