奇怪的行为的foreach [英] Strange behavior Of foreach

查看:111
本文介绍了奇怪的行为的foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < PHP
  $一个=阵列('一个','B','C','D');  的foreach($ A为&放大器; $ V){}
  的foreach($ A $为V){}  的print_r($ A);
?>

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

 阵列

    [0] =>一个
    [1] => b
    [2] => C
    [3] => C

有人可以解释这样对我?


解决方案

这是证据充分的PHP行为
参见php.net的页面的foreach

警告

  

警告


  
  

$值参考的最后数组元素连的的foreach 的循环之后仍然存在。建议由未设置()。消灭它。


  $ a =阵列('A','B','C','D');的foreach($ A为&放大器; $ V){}
未设置($ V);
的foreach($ A $为V){}的print_r($ A);

修改

尝试在一步一步的指导,什么是真正发生在这里。

  $ a =阵列('A','B','C','D');
的foreach($ a作为&放大器; $ⅴ){} //第1次迭代$ v是到$基准的[0]('a')的
的foreach($ a作为&放大器; $ⅴ){} //第二迭代$ v是到$一个[1]('B')的参考
的foreach($ A为&放大器; $ V){} // 3迭代$ V是一个$ [2]('C')的参考
的foreach($ a作为&放大器; $ⅴ){} //第四迭代$ v是到$一个[3](D)的参考                          //在foreach循环结束时,
                          // $ V仍然是一个$ [3](D)的参考的foreach($ A $为V){} //第1次迭代$ V(仍然是一个以$参考[3])
                          //设置为$一个[0]('a')的一个值。
                          //因为它是到$基准的[3],
                          //它设置一个$ [3]为'A'。
的foreach($ A $为V){} //第二迭代$ V(仍然是一个以$参考[3])
                          //设置为$一个[1]('B')的值。
                          //因为它是到$基准的[3],
                          //它设置一个$ [3]为'B'。
的foreach($ A $为V){} // 3迭代$ V(仍然是一个以$参考[3])
                          //设置为$一个[2]('C')的值。
                          //因为它是到$基准的[3],
                          //它设置一个$ [3]C。
的foreach($ A $为V){} // 4迭代$ ​​V(仍然是一个以$参考[3])
                          //设置为自变量$ a [3]('C'的值
                          //最后一次迭代)。
                          //因为它是到$基准的[3],
                          //它设置一个$ [3]C。

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

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

  print_r($a);
?>

I think it's a normal program but this is the output I am getting:

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

Can someone please explain this to me?

解决方案

This is well-documented PHP behaviour See the warning on the foreach page of php.net

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

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

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

print_r($a);

EDIT

Attempt at a step-by-step guide to what is actually happening here

$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'.

这篇关于奇怪的行为的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆