$a = [ "a"=>["cc"=>11] , "b"=>["cc"=>22] ];echo "\n".$a['a']["cc"].",".$a['b']["cc"];$i=1;//(1)拷贝,$a无变化foreach($a as $k=>$v) { $v['cc']=$i; $i+=1;}echo "\n".$a['a']["cc"].",".$a['b']["cc"];//(2)引用foreach($a as $k=>&$v) { $v['cc']=$i; $i+=1;}echo "\n".$a['a']["cc"].",".$a['b']["cc"];//(3)拷贝foreach($a as $k=>$v) { $a[$k]["cc"]=$i; $i+=1;}echo "\n".$a['a']["cc"].",".$a['b']["cc"];
The default foreach is a copy array, which requires twice times the memory, is the performance very low AH? Why would you want to design this?
If the array is very large, is it better to use the reference method? What are their strengths and weaknesses?
Reply content:
$a = [ "a"=>["cc"=>11] , "b"=>["cc"=>22] ];echo "\n".$a['a']["cc"].",".$a['b']["cc"];$i=1;//(1)拷贝,$a无变化foreach($a as $k=>$v) { $v['cc']=$i; $i+=1;}echo "\n".$a['a']["cc"].",".$a['b']["cc"];//(2)引用foreach($a as $k=>&$v) { $v['cc']=$i; $i+=1;}echo "\n".$a['a']["cc"].",".$a['b']["cc"];//(3)拷贝foreach($a as $k=>$v) { $a[$k]["cc"]=$i; $i+=1;}echo "\n".$a['a']["cc"].",".$a['b']["cc"];
The default foreach is a copy array, which requires twice times the memory, is the performance very low AH? Why would you want to design this?
If the array is very large, is it better to use the reference method? What are their strengths and weaknesses?