<?php $students = array( '2010'=>array('令狐冲',"59"), '2011'=>array('林平之',"44"), '2012'=>array('曲洋',"89"), '2013'=>array('任盈盈',"92"), '2014'=>array('向问天',"93"), '2015'=>array('任我行',"87"), '2016'=>array('冲虚',"58"), '2017'=>array('方正',"74"), '2018'=>array('岳不群',"91"), '2019'=>array('宁中则',"90"), );//10个学生的学号、姓名、分数,用数组存储 foreach($students as $key=>$val) { //使用循环结构遍历数组,获取学号 echo $key; //输出学号 echo ":"; //循环输出姓名和分数 foreach($val as $v=>$u) { echo $v."-".$u; } echo "<br />"; } ?>
输出结果为:
2010:0-令狐冲1-59
2011:0-林平之1-44
2012:0-曲洋1-89
2013:0-任盈盈1-92
2014:0-向问天1-93
2015:0-任我行1-87
2016:0-冲虚1-58
2017:0-方正1-74
2018:0-岳不群1-91
2019:0-宁中则1-90
因为名字的坐标是0,成绩的坐标是1;$v被用作key了,$u被用作值。所以遍历的是array(‘令狐成’,‘59’)这样的嵌套数组
foreach($val as $v=>$u),你写的这个是取下标的遍历,所以会出现0和1。改为foreach($val as $v)就不会出现了。