欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 奇怪现象,求大神解答

程序员文章站 2022-05-06 08:56:19
...
$aBU = array (
array ( 'id' => '0', ),
array ( 'id' => '1', ),
array ( 'id' => '2', ),
array ( 'id' => '3', ),
);
echo "

原来数据"; print_r($aBU); echo "
"
; foreach($aBU as &$bu){ $bu['dept'] = rand(100, 999); } //unset($bu); echo "

加入dept"; print_r($aBU); echo "
"
; echo "

重新遍历"; foreach($aBU as $key => $bu){ print_r($bu); echo "
"
; }

输出:

原来数据

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教程有兴趣的朋友有所帮助。