dtmwnqng38644 2012-01-17 21:19
浏览 32
已采纳

用引用理解foreach逻辑 - 为什么第一个元素被改为'two',第二个元素被改为'三',第三个被改为'three3'?

I am trying to understand the ins and outs of how references work, but I got stuck trying to follow the programming logic of what exactly this code is doing as it loops through the array. I'm trying to understand what is happening, step by step.

I have read through the PHP.net posts, and understand that when using references in the foreach of an array you have to unset the variable. I also understand that the code below is not the best code. I'm just using it for learning purposes to follow programming flow logic on how the php interpreter runs through different code.

My question is, what is this code doing each time it loops through this array, to cause it to output the following if I do not unset($v)? In other words, what is it doing step by step to get the array to have 'two' as the first element, 'three', as the second element, and 'three3' as the third?

$arr = array(1=>'one',2=>'two',3=>'three');

foreach($arr as $k=>$v){

   $v = &$arr[$k];
   $v .= $k;
   echo $v . "
";
   //unset($v) .... if I use unset($v) here, then the resulting $arr is correct.
}

output is...

one1
two2
three3

Array
(
[1] => two
[2] => three
[3] => three3
)

Thanks so much for the help!!

  • 写回答

1条回答 默认 最新

  • douyun3887 2012-01-17 21:31
    关注

    First time through the loop

    foreach($arr as $k=>$v){ // Sets $v to a value of "one"
       $v =& $arr[$k];       //  Sets $v as a reference to $arr[1] ("one")
       $v .= $k;             //  Sets $v (and hence also $arr[1]) to "one1"
    

    Second time through the loop

    foreach($arr as $k=>$v){ //  Sets $v to a value of "two"... 
                             //      because $v is already set as a reference to $arr[1] from the previous loop, 
                             //      this changes $arr[1] to a value of "two"
       $v =& $arr[$k];       //  Sets $v as a reference to $arr[2] ("two")
                             //  It no longer references $arr[1] so $arr[1] will not be changed any further
       $v .= $k;             //  Sets $v (and hence also $arr[2]) to "two2"
    

    Third time through the loop

    foreach($arr as $k=>$v){ //  Sets $v to a value of "three"... 
                             //      because $v is already set as a reference to $arr[2] from the previous loop, 
                             //      this changes $arr[2] to a value of "three"
       $v =& $arr[$k];       //  Sets $v as a reference to $arr[3] ("three")
                             //  It no longer references $arr[2] so $arr[2] will not be changed any further
       $v .= $k;             //  Sets $v (and hence also $arr[3]) to "three3"
    

    If you use the unset()

    First time through the loop

    foreach($arr as $k=>$v){ // Sets $v to a value of "one"
       $v =& $arr[$k];       //  Sets $v as a reference to $arr[1] ("one")
       $v .= $k;             //  Sets $v (and hence also $arr[1]) to "one1"
       unset($v);            //  Unsets $v as a reference, it no longer points to $arr[1]
    

    Second time through the loop

    foreach($arr as $k=>$v){ //  Sets $v to a value of "two"... 
                             //      As $v is no longer set as a reference to $arr[1], 
                             //      this leaves $arr[1] unchanged by this loop
       $v =& $arr[$k];       //  Sets $v as a reference to $arr[2] ("two")
       $v .= $k;             //  Sets $v (and hence also $arr[2]) to "two2"
       unset($v);            //  Unsets $v as a reference, it no longer points to $arr[2]
    

    Third time through the loop

    foreach($arr as $k=>$v){ //  Sets $v to a value of "three"... 
                             //      As $v is no longer set as a reference to $arr[2], 
                             //      this leaves $arr[2] unchanged by this loop
       $v =& $arr[$k];       //  Sets $v as a reference to $arr[3] ("three")
       $v .= $k;             //  Sets $v (and hence also $arr[3]) to "three3"
       unset($v);            //  Unsets $v as a reference, it no longer points to $arr[3]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)