ITPub博客

首页 > IT职业 > IT生活 > 哈哈,PHP中使用foreach和引用导致程序BUG及解决办法

哈哈,PHP中使用foreach和引用导致程序BUG及解决办法

IT生活 作者:aqiandao 时间:2016-07-08 16:32:42 0 删除 编辑

  PHP中使用foreach和引用导致程序BUG及解决办法

  foreach( $array as &$item ) { }

  print_r( $array );

  foreach( $array as $item ) { }

  print_r( $array );

  ?>1234567

  预期结果:

  Array

  (

  [0] => 1

  [1] => 2

  [2] => 3

  )

  Array

  (

  [0] => 1

  [1] => 2

  [2] => 3

  )123456789101112

  实际结果:

  Array

  (

  [0] => 1

  [1] => 2

  [2] => 3

  )

  Array

  (

  [0] => 1

  [1] => 2

  [2] => 2

  )123456789101112

  在执行第一个使用引用的 foreach 时, 一开始, $v 指向 $ar[0] 的存储空间,空间内存储着 1 , foreach 结束时, $v 指向 $ar[2] 的存储空间,空间内存储着 3 。 下面要开始执行第二个 foreach 了,注意和第一个 foreach 不同, 第二个 foreach 没有使用引用,那么就是赋值方式, 即将 $ar 的值依次 赋值 给 $v 。 进行到第一个元素时,要将 $ar[0] 赋值给 $v 。 问题就在这里,由于刚刚执行完第一个 foreach, $v 不是一个新变量,而是已经存在的、指向 $ar[2] 的那个 引用 , 如此一来,对 $v 进行赋值的时候,就将 $ar[0] = 1 写入了 $ar[2] 的实际存储空间, 相当于对 $ar[2] 进行赋值。 依此类推,第二个 foreach 执行的结果, 就是数组的最后一个元素变成了倒数第二个元素的值。

  解决办法:

  两个foreach中间加一个unset($item);

  $array = array(1,2,3);

  foreach( $array as &$item ) { }

  print_r( $array );

  unset($item);

  foreach( $array as $item ) { }

  print_r( $array );

  ?>12345678

  或者第二个foreach不要用$item,换成别的变量

  $array = array(1,2,3);

  foreach( $array as &$item ) { }

  print_r( $array );

  foreach( $array as $rs ) { }

  print_r( $array );

  ?>

  http://www.99inf.com/jinrong/yhbx/806051.html

  http://www.99inf.com/jinrong/yhbx/806057.html

  http://www.99inf.com/jinrong/yhbx/806082.html

  http://www.99inf.com/jinrong/yhbx/806093.html

  http://www.99inf.com/jinrong/yhbx/806116.html

  http://www.99inf.com/jinrong/yhbx/806136.html

  http://www.99inf.com/jinrong/yhbx/806156.html

  http://www.99inf.com/jinrong/yhbx/806170.html

  http://www.99inf.com/jinrong/yhbx/806179.html

  http://www.99inf.com/jinrong/yhbx/806189.html

  http://site.leshou.com/s/28730540.html

  http://site.leshou.com/s/28730817.html

  http://site.leshou.com/s/28730962.html

  http://site.leshou.com/s/28731285.html

  http://site.leshou.com/s/28731593.html

  http://site.leshou.com/s/28731961.html

  http://site.leshou.com/s/28732443.html

  http://site.leshou.com/s/28732824.html

  http://site.leshou.com/s/28733202.html

  http://site.leshou.com/s/28733376.html

  http://shiyou.huangye88.com/xinxi/87625257.html

  http://shiyou.huangye88.com/xinxi/87625635.html

  http://shiyou.huangye88.com/xinxi/87626116.html

  http://shiyou.huangye88.com/xinxi/87626525.html

  http://shiyou.huangye88.com/xinxi/87626962.html

  http://beijing.edeng.cn/jiedaoxinxi/246323582.html

  http://beijing.edeng.cn/jiedaoxinxi/246324436.html

  http://beijing.edeng.cn/jiedaoxinxi/246344992.html

  http://beijing.edeng.cn/jiedaoxinxi/246345809.html

  http://beijing.edeng.cn/jiedaoxinxi/246346536.html

  https://www.wang1314.com/doc/topic-1238214-1.html

  http://blog.163.com/m13146230227_3/blog/static/260660031201667222895/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166722813281/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166723453652/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166723859691/

  http://blog.163.com/m13146230227_3/blog/static/2606600312016672437737/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166724551313/

  http://blog.163.com/m13146230227_3/blog/static/2606600312016672507881/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166725345691/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166725530140/

  http://blog.163.com/m13146230227_3/blog/static/26066003120166725850276/

  http://blog.sina.com.cn/s/blog_14dc17b280102weep.html

  http://blog.sina.com.cn/s/blog_14dc17b280102weeq.html

  http://blog.sina.com.cn/s/blog_14dc17b280102weer.html

  http://blog.sina.com.cn/s/blog_14dc17b280102wees.html

  http://blog.sina.com.cn/s/blog_14dc17b280102weeu.html

  http://blog.sina.com.cn/s/blog_14dc17b280102weew.html

  http://blog.sina.com.cn/s/blog_14dc17b280102weex.html

  http://blog.sina.com.cn/s/blog_14dc17b280102weey.html

  http://blog.eastmoney.com/m6711124611311778/blog_441574295.html

  http://blog.eastmoney.com/m6711124611311778/blog_441577660.html

  http://blog.eastmoney.com/m6711124611311778/blog_441580318.html

  http://blog.eastmoney.com/m6711124611311778/blog_441582275.html

  http://blog.eastmoney.com/m6711124611311778/blog_441586047.html

  http://blog.eastmoney.com/m6711124611311778/blog_441588435.html

  http://blog.eastmoney.com/m6711124611311778/blog_441592446.html

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20571

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20570

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20569

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20568

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20566

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20564

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20563

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20562

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=20561

  http://wonyen.net/myhome/article.aspx?id=aqiandao&tid=19282

  http://www.meiyuanxing.com/html/197-23510.html

  http://www.meiyuanxing.com/html/197-23509.html

  http://www.meiyuanxing.com/html/197-23545.html

  http://www.meiyuanxing.com/html/197-23544.html

  http://www.meiyuanxing.com/html/197-23543.html

  http://www.meiyuanxing.com/html/197-23542.html

  http://www.meiyuanxing.com/html/197-23541.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30065054/viewspace-2121738/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-12-15

  • 博文量
    79
  • 访问量
    32764