krsort()
方法krsort()函数用于根据键对降序排列的关联数组进行排序,因为我们知道关联数组包含键和值,因此该方法根据键对数组进行排序。
它不返回已排序的数组,而是对输入数组进行排序。
语法:
krsort(array, [mode]);
这里,
数组是输入数组
mode是一个可选参数,默认值为0,它具有以下值:
0 –用于正常比较项目
1 –用于比较项目数字
2 –用于比较项目为字符串
3 –用于比较项目作为当前区域设置字符串
4 –用于比较项目作为字符串(自然顺序)
例子:
Input: $person = array( "radib" => 21, "amit" => 21, "abhi" => 20, "prem" => 27, "manju" => 25 ); Output: sorted array... Array ( [radib] => 21 [prem] => 27 [manju] => 25 [amit] => 21 [abhi] => 20 )
PHP代码:
<?php $person = array( "radib" => 21, "amit" => 21, "abhi" => 20, "prem" => 27, "manju" => 25 ); print ("unsorted array...\n"); print_r ($person); //排序...- krsort($person); print ("sorted array...\n"); print_r ($person); ?>
输出结果
unsorted array... Array ( [radib] => 21 [amit] => 21 [abhi] => 20 [prem] => 27 [manju] => 25 ) sorted array... Array ( [radib] => 21 [prem] => 27 [manju] => 25 [amit] => 21 [abhi] => 20 )