首頁  >  文章  >  後端開發  >  php 奇怪現象,求大神解答

php 奇怪現象,求大神解答

WBOY
WBOY原創
2016-07-29 09:14:181000瀏覽
<span><?php
</span><span >$aBU </span><span >= </span><span >array </span><span >(
</span><span ></span><span >array </span><span >( </span><span >'id' </span><span >=> </span><span>'0'</span><span>, </span><span>)</span><span>,
</span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'1'</span><span>, </span><span>)</span><span>,
</span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'2'</span><span>, </span><span>)</span><span>,
</span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'3'</span><span>, </span><span>)</span><span>,
</span><span>)</span><span>;
</span><span>echo </span><span>"<h3></span><span color: background-color:>原来数据</span><span></h3>"</span><span>;
</span><span>print_r(</span><span>$aBU</span><span>)</span><span>;
</span><span>echo </span><span>"<hr />"</span><span>;
</span><span></span><span>foreach</span><span>(</span><span>$aBU </span><span>as  </span><span>&</span><span>$bu</span><span>){
</span><span></span><span>$bu</span><span>[</span><span>'dept'</span><span>] = rand(</span><span>100</span><span>, </span><span>999</span><span>)</span><span>;
</span><span>}
</span><span>//unset($bu);
</span><span>echo </span><span>"<h3></span><span color: background-color:>加入</span><span>dept</span><span color: background-color:>后</span><span></h3>"</span><span>;
</span><span>print_r(</span><span>$aBU</span><span>)</span><span>;
</span><span>echo </span><span>"<hr />"</span><span>;
</span><span></span><span></span><span>echo </span><span>"<h3></span><span color: background-color:>重新遍历</span><span></h3>"</span><span>;
</span><span>foreach</span><span>(</span><span>$aBU </span><span>as </span><span>$key </span><span>=> </span><span>$bu</span><span>){
</span><span>    print_r(</span><span>$bu</span><span>)</span><span>;
</span><span></span><span>echo </span><span>"<br />"</span><span>;
</span><span>}</span>

輸出:

原來資料

Array( [0] => Array ( [id] => 0 ) [1] => Array ( [id] => 1 ) [2] => Array ( [id] = > 2 ) [3] => Array ( [id] => 3 ))

加入dept後

Array( [0] => Array ( [id] => 0 [dept] => 783 ) [1] => Array ( [id] => 1 [dept] => 683 ) [2 ] => Array ( [id] => 2 [dept] => 442 ) [3] => Array ( [id] => 3 [dept] => 121 ))

重新遍歷,最後一個元素的竟然得到的竟然是前一個元素?

Array( [id] => 0 [dept] => 783)
Array( [id] => 1 [dept] => 683)
Array( [id] => 2 [dept] => 442)
Array( [id] => 2 [dept] => 442)

以上就介紹了php 奇怪現象,求大神解答,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn