2,"name"=>"lisi","age"=>28);//一维关联数组,不能用for循环遍历 //下面是二维关联数组 $arr_3 = array(array("id"=>2,"name"=>"lucy","age"=>26), array("id"=>3,"name"=>"lili","age"=>27), array("id"=>4,"name"=>"lisi","age"=>28), ); //一维索引数组可以用for循环遍历出数组内容 for ($i=0; $i "; //一维索引数组还可以用foreach循环遍历出数组内容 foreach ($arr_1 as $key => $val) { echo $val.""; } /* 1 zhangsan 25 */ echo "
"; //一维关联数组不能用for循环,只能用foreach foreach ($arr_2 as $key => $val) { echo $key."-->".$val.""; } /* id-->2 name-->lisi age-->28 */ echo "
"; //二维关联数组用foreach循环遍历 foreach ($arr_3 as $val) { echo $val['id']." "; echo $val['name']." "; echo $val['age'].""; } echo "
"; /* 2 lucy 26 3 lili 27 4 lisi 28 */ //二维关联数组用foreach循环2次遍历 echo '
'; echo 'IDNAMEAGE'; foreach ($arr_3 as $key=>$row) { echo ""; if(is_array($row)){ foreach ($row as $col) { echo " ".$col.""; } }else{ echo " | ".$key.':'.$row.""; } echo ""; } echo "";?> |