krsort
PHP krsort()函数 (PHP krsort() function)
krsort() function is used to sort an associative array in descending order based on the keys, as we know that an associative array contains keys and values, this method sorts an array according to the keys.
krsort()函数用于根据键对降序排列的关联数组进行排序,因为我们知道关联数组包含键和值 ,因此该方法根据键对数组进行排序。
It does not return a sorted array, it sorts the input array.
它不返回已排序的数组,而是对输入数组进行排序。
Syntax:
句法:
krsort(array, [mode]);
Here,
这里,
array is an input array
数组是输入数组
mode is an optional parameter, its default value is 0, it has following values:
mode是一个可选参数,其默认值为0,它具有以下值:
0 – It is used to compare items normally
0 –用于正常比较项目
1 – It is used to compare items numerically
1 –用于数字比较项目
2 – It is used to compare items as strings
2 –用于比较项目作为字符串
3 – It is used to compare items as current locale strings
3 –用于比较项目作为当前区域设置字符串
4 – It is used to compare items as strings (natural ordering)
4 –用于将项目作为字符串进行比较(自然顺序)
Examples:
例子:
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 code:
PHP代码:
<?php
$person = array(
"radib" => 21,
"amit" => 21,
"abhi" => 20,
"prem" => 27,
"manju" => 25
);
print ("unsorted array...\n");
print_r ($person);
//sorting...
krsort($person);
print ("sorted array...\n");
print_r ($person);
?>
Output
输出量
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
)
翻译自: https://www.includehelp.com/php/krsort-function-with-example.aspx
krsort