說一個極有可能在工作上遇到的問題-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教程有興趣的朋友有所幫助。