当前位置:Gxlcms > PHP教程 > php奇怪现象,求大神解答

php奇怪现象,求大神解答

时间:2021-07-01 10:21:17 帮助过:5人阅读

  1. <span><!--?php
  2. </span--><span>$aBU </span><span>= </span><span>array </span><span>(
  3. </span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'0'</span><span>, </span><span>)</span><span>,
  4. </span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'1'</span><span>, </span><span>)</span><span>,
  5. </span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'2'</span><span>, </span><span>)</span><span>,
  6. </span><span></span><span>array </span><span>( </span><span>'id' </span><span>=> </span><span>'3'</span><span>, </span><span>)</span><span>,
  7. </span><span>)</span><span>;
  8. </span><span>echo </span><span>"<h3><span "font-family:'宋体';="" color:#6a8759;="" background-color:#232525"="">原来数据</span><span></span></h3>"</span><span>;
  9. </span><span>print_r(</span><span>$aBU</span><span>)</span><span>;
  10. </span><span>echo </span><span>"<hr>"</span><span>;
  11. </span><span></span><span>foreach</span><span>(</span><span>$aBU </span><span>as </span><span>&</span><span>$bu</span><span>){
  12. </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>;
  13. </span><span>}
  14. </span><span>//unset($bu);
  15. </span><span>echo </span><span>"<h3><span "font-family:'宋体';="" color:#6a8759;="" background-color:#232525"="">加入</span><span>dept</span><span "font-family:'宋体';="" color:#6a8759;="" background-color:#232525"="">后</span><span></span></h3>"</span><span>;
  16. </span><span>print_r(</span><span>$aBU</span><span>)</span><span>;
  17. </span><span>echo </span><span>"<hr>"</span><span>;
  18. </span><span></span><span></span><span>echo </span><span>"<h3><span "font-family:'宋体';="" color:#6a8759;="" background-color:#232525"="">重新遍历</span><span></span></h3>"</span><span>;
  19. </span><span>foreach</span><span>(</span><span>$aBU </span><span>as </span><span>$key </span><span>=> </span><span>$bu</span><span>){
  20. </span><span> print_r(</span><span>$bu</span><span>)</span><span>;
  21. </span><span></span><span>echo </span><span>"<br>"</span><span>;
  22. </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教程有兴趣的朋友有所帮助。

人气教程排行