说一个极有可能在工作中遇到的问题——foreach的引用
<span>$arr</span> = <span>range</span>(1,3<span>); </span><span>//</span><span>[1,2,3]</span> <span>foreach</span>(<span>$arr</span> <span>as</span> &<span>$val</span><span>) { } </span><span>foreach</span>(<span>$arr</span> <span>as</span> <span>$val</span><span>) { } </span><span>print_r</span>(<span>$arr</span><span>); </span>
上面这段代码输出什么呢,神奇的竟然是下面这样,这个我在工作中遇到过一次,当时弄了半天没弄出个所以然,只是找到了解决办法,这个问题解决办法有两个:
<span>Array</span><span> ( [</span>0] => 1<span> [</span>1] => 2<span> [</span>2] => 2<span> )</span>
下面这两种方式都可以解决上面这个问题:
<span>//</span><span>方法1</span> <span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) { } </span><span>unset</span>(<span>$value</span><span>); </span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$value</span><span>) { } </span><span>print_r</span>(<span>$arr</span><span>); </span><span>//</span><span>[1,2,3] //方法2</span> <span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) { } </span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$val</span><span>) { } </span><span>print_r</span>(<span>$arr</span><span>); </span><span>//</span><span>[1,2,3] //方法3</span> <span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) { } </span><span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) { } </span><span>print_r</span>(<span>$arr</span><span>); </span><span>//</span><span>[1,2,3]</span>
方法1在官方的手册中也可以看到http://php.net/manual/en/control-structures.foreach.php,文章中专门有个tips提示这一点
方法2和方法3为什么也ok呢,看了下面就知道了,其实就是引用在”作祟“,foreach中的&$value是一个指向数组中某一个元素的指针变量,先看下面个,这个很好理解,tmp是var的引用,指向的是var的存储空间,当tmp改变的时候,var也改变
<span>$var</span> = 123<span>; </span><span>$tmp</span> = &<span>$var</span><span>; </span><span>$tmp</span> = 200<span>; </span><span>echo</span> <span>$var</span><span>; </span><span>//</span><span>200</span>
看看下面这张盗来的图(哈哈,原文链接贴在下面),对上面这个更好的理解
好了,这个ok了之后再看下面的(我不知道上面这种图用什么软件画的,只能手绘了,囧~~),先看第一个foreach:
第二次foreach:
这个时候就很容易知道其实第二次foreach循环,数组中最后一个元素值真是多变啊~~,从第一个元素一直变到倒数第二个,要解决这个问题,很简单,切断$value与第二次foreach的关系,因此,unset可以,换个变量名也可以,或者将指针重置也可以(方法3),好了,至此,这个问题到这个地方基本可以理解个所以然了。
好,再看看下面这个官方文档下面的评论,应该可以很容易理解了
<span>php </span><span>$arr1</span> = <span>array</span>("a" => 1, "b" => 2, "c" => 3<span>); </span><span>$arr2</span> = <span>array</span>("x" => 4, "y" => 5, "z" => 6<span>); </span><span>foreach</span> (<span>$arr1</span> <span>as</span> <span>$key</span> => &<span>$val</span><span>) {} </span><span>foreach</span> (<span>$arr2</span> <span>as</span> <span>$key</span> => <span>$val</span><span>) {} </span><span>var_dump</span>(<span>$arr1</span><span>); </span><span>var_dump</span>(<span>$arr2</span><span>); </span>?><span> The output is</span>: <span>array</span>(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(6<span>) } </span><span>array</span>(3) { ["x"]=> int(4) ["y"]=> int(5) ["z"]=> int(6) }
参考文章:
http://www.cnblogs.com/CraryPrimitiveMan/p/4030748.html#3085766
http://www.jb51.net/article/39299.htm
本文版权归作者iforever(luluyrt@163.com)所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
以上就介绍了PHP中的一个”坑“,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。