本文主要介绍的php数组函数主要有:sort、rsort、asort、arsort、ksort、krsort
测试数据定义一个关联数组如下:
$data=[ 'f'=>123, 'b'=>789, 'g'=>456, 'd'=>111 ]; 这里写数组函数($data); foreach($data as $key => $val){ echo $key."=>".$val."<br>"; }
(1)sort:升序排序:原有的键会从0开始被覆盖,按照值进行升序排序,输出结果为
0=>111 1=>123 2=>456 3=>789
(2)rsort:降序排序,原理与sort一样,但是是按照降序排序,输出结果为
0=>789 1=>456 2=>123 3=>111
(3)asort:升序排序,但是不改变原有的键,按照值进行升序排序,输出结果为
d=>111 f=>123 g=>456 b=>789
(4)arsort:降序排序,同样不改变原有的键,但是是按照降序排序,输出结果为
b=>789 g=>456 f=>123 d=>111
(5)ksort:升序排序,根据键从小到大排序,不受数组的值影响,输出结果为
b=>789 d=>111 f=>123 g=>456
(6)krsort:降序排序,根据键从大到小排序,同样不受数组的值影响,输出结果为
g=>456 f=>123 d=>111 b=>789