<?php /** * Created by phpstorm * Author: Aka * Date: 2019/12/6 * Time: 17:06 **/ //索引数组 $arr1 = array('this','is','first','array','!'); //关联数组 $arr2 = array('firstName'=>'Aka','lastName'=>'Jack','age'=>20,'address'=>"湖南湘潭"); $arr3 = array('iphoneX'=>5000,'mi9'=>4600,'iphone8'=>6000,'honor_v12'=>4396); $arr4 = array('13'=>5000,26=>'Lx','ip'=>6000,4396=>'clearLove'); //遍历数组 echo '通过for遍历索引数组','<br>'; for($i=0;$i<count($arr1);$i++){ echo "arr1[$i]=".$arr1[$i],'<br>'; } echo '通过foreach()遍历关联数组','<br>'; foreach ($arr2 as $key => $value){ echo $key,'=>',$value,'<br>'; } var_dump($arr2); echo '<hr>','以下是数组的排序函数'; echo '用sort()函数进行升序排序','<br>'; sort($arr3); foreach ($arr3 as $k =>$v){ // 经过sort()之后$arr就会变成一个新的数组,它的key会变成索引,即不保持索引关系 echo '<br>','key='.$k,'<br>','value='.$v,'<br>'; } echo '对arr3进行降序排序','<br>'; rsort($arr3); foreach ($arr3 as $key => $value){ echo '<br>','key='.$key,'<br>','value='.$value,'<br>'; } echo '对键名进行升序排列','<br>'; ksort($arr4); foreach ($arr4 as $key => $value){ echo '<br>','key='.$key,'<br>','value='.$value,'<br>'; } echo '对键名进行降序排列','<br>'; krsort($arr4); foreach ($arr4 as $key => $value){ echo '<br>','key='.$key,'<br>','value='.$value,'<br>'; } echo '保持索引关系的同时对值进行升序排列','<br>'; asort($arr4); foreach ($arr4 as $key => $value){ echo '<br>','key='.$key,'<br>','value='.$value,'<br>'; } echo '保持索引关系的同时对值进行降序序排列','<br>'; arsort($arr4); foreach ($arr4 as $key => $value){ echo '<br>','key='.$key,'<br>','value='.$value,'<br>'; }