<?php $data1 = ["11","12","13"]; $data2 = ["14","15","16"]; $all = 0; foreach($data1 as &$x){ echo $x, ' ', $x; echo "
"; } //$x = 0; echo "\$x is ",$x; echo "-------------
"; $all2 = 0; //把$x这个索引2当作值2来使用了,因为在上面的循环它就是个值,在下面的循环里它又是个索引 foreach($data2 as $x=>$y){ echo $x,'==>',$y,' data1[', $x,'] = ',$data1[$x]; echo "
"; $all2 += ($y+$data1[$x]); //原意是计算:1+4 + 2+5 + 3+6 的总和,结果应该是21 } echo $all2;
"; } //$x = 0; echo "\$x is ",$x; echo "-------------
"; $all2 = 0; //把$x这个索引2当作值2来使用了,因为在上面的循环它就是个值,在下面的循环里它又是个索引 foreach($data2 as $x=>$y){ echo $x,'==>',$y,' data1[', $x,'] = ',$data1[$x]; echo "
"; $all2 += ($y+$data1[$x]); //原意是计算:1+4 + 2+5 + 3+6 的总和,结果应该是21 } echo $all2;