<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] => 배열 ( [id] => 2 ) [3] => 배열 ( [id] => 3 ))부서 추가 후
Array( [0] => Array ( [id] => 0 [dept] => 783 ) [1] => Array ( [id] => ; 1 [dept] => 683 ) [2] => 배열 ( [id] => 2 [dept] => 442 ) [3] => 배열 ( [id] => 3 ] => 121 ))다시 트래버스합니다. 마지막 요소가 실제로 이전 요소를 가져왔나요?
이상은 PHP의 이상한 현상을 소개한 내용입니다. 관련 내용을 포함하여 마스터에게 답변을 요청하시기 바랍니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.