使用引用时 foreach 的奇怪行为: foreach ($a为 &$v) { ... }

2022-08-30 10:28:16
<?php
  $a = array('a', 'b', 'c', 'd');

  foreach ($a as &$v) { }
  foreach ($a as $v) { }

  print_r($a);
?>

我认为这是一个正常的程序,但这是我得到的输出:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => c
)

有人可以向我解释一下吗?


答案 1

这是有据可查的 PHP 行为,请参阅 php.net

警告

$value的引用和最后一个数组元素即使在 foreach 循环之后仍保留。建议通过 unset() 销毁它。

$a = array('a', 'b', 'c', 'd');

foreach ($a as &$v) { }
unset($v);
foreach ($a as $v) { }

print_r($a);

编辑

尝试分步指南,了解这里实际发生的事情

$a = array('a', 'b', 'c', 'd');
foreach ($a as &$v) { }   // 1st iteration $v is a reference to $a[0] ('a')
foreach ($a as &$v) { }   // 2nd iteration $v is a reference to $a[1] ('b')
foreach ($a as &$v) { }   // 3rd iteration $v is a reference to $a[2] ('c')
foreach ($a as &$v) { }   // 4th iteration $v is a reference to $a[3] ('d')

                          // At the end of the foreach loop,
                          //    $v is still a reference to $a[3] ('d')

foreach ($a as $v) { }    // 1st iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[0] ('a').
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'a'.
foreach ($a as $v) { }    // 2nd iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[1] ('b').
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'b'.
foreach ($a as $v) { }    // 3rd iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[2] ('c').
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'c'.
foreach ($a as $v) { }    // 4th iteration $v (still a reference to $a[3]) 
                          //    is set to a value of $a[3] ('c' since 
                          //       the last iteration).
                          //    Because it is a reference to $a[3], 
                          //    it sets $a[3] to 'c'.

答案 2

第一个 foreach 循环不会对数组进行任何更改,正如我们所期望的那样。但是,它确实会导致被分配一个对 每个元素的引用,因此,当第一个循环结束时,实际上是对 的引用。$v$a$v$a[2]

一旦第二个循环开始,现在就分配了每个元素的值。但是,已经是一个引用,因此分配给它的任何值都将自动复制到数组的最后一个元素中!$v$v$a[2];

因此,在第一次迭代中,将变为零,然后是一,然后再次变为一,有效地复制到自身上。要解决此问题,应始终取消设置在按引用 foreach 循环中使用的变量,或者更好的是,完全避免使用前者。$a[2]