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