Home > Backend Development > PHP Tutorial > php strange phenomenon, please give me an answer

php strange phenomenon, please give me an answer

WBOY
Release: 2016-07-29 09:14:18
Original
1017 people have browsed it
<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>
Copy after login

Output:

Original data

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

After adding 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 ) )

Traverse again, The last element actually got the previous element?

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

The above has introduced the strange phenomenon of PHP. Please ask the master to answer it, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template