<?php $students = array( '2010'=>array('令狐冲',"59","f"), '2011'=>array('林平之',"44","f"), '2012'=>array('曲洋',"89","m"), '2013'=>array('任盈盈',"92","m"), '2014'=>array('向问天',"93","f"), '2015'=>array('任我行',"87","m"), '2016'=>array('冲虚',"58","f"), '2017'=>array('方正',"74","d"), '2018'=>array('岳不群',"91","e"), '2019'=>array('宁中则',"90","s"), );//10个学生的学号、姓名、分数,用数组存储 foreach($students as $key => $val) { //使用循环结构遍历数组,获取学号 echo $key; //输出学号 echo ":"; //循环输出姓名和分数 foreach($val as $v) { //echo $v; } echo "<br />"; } { echo $v."<br />"; } ?>
<?php $students = array( '2010'=>array('令狐冲',"59","f"), '2011'=>array('林平之',"44","f"), '2012'=>array('曲洋',"89","m"), '2013'=>array('任盈盈',"92","m"), '2014'=>array('向问天',"93","f"), '2015'=>array('任我行',"87","m"), '2016'=>array('冲虚',"58","f"), '2017'=>array('方正',"74","d"), '2018'=>array('岳不群',"91","e"), '2019'=>array('宁中则',"90","s"), );//10个学生的学号、姓名、分数,用数组存储 $a=1; //计数器 foreach($students as $key => $val){ //使用循环结构遍历数组,获取学号 // echo $key; //输出学号 // echo ":"; //循环输出姓名和分数 foreach($val as $v){ echo "<br />"; echo "第".$a++.'次 $v的值为'.":".$v; } echo "<br />"; } { echo $v."<br />"; //当最后遍历完毕后,$V的值为s } echo var_dump($v); //string 's' (length=1) ?>
其实不难理解,我们把每一次遍历时,把 $v 这个变量打印出来看一下就明白了。
当最后一次遍历后,变量$v 所存放的值为 ‘s’