rsort和asort
PHP rsort()函数 (PHP rsort() function)
rsort() function is used to sort an indexed array in descending order.
rsort()函数用于按降序对索引数组进行排序。
If array elements are numeric, it sorts based on the numbers, if array elements are the string, it sorts based on the alphabets and if the array contains numeric values and text/strings, it sorts elements based on the alphabets.
如果数组元素是数字,则根据数字排序;如果数组元素是字符串,则根据字母排序;如果数组包含数字值和文本/字符串,则根据字母排序。
It does not return a sorted array, it sorts the input array.
它不返回已排序的数组,而是对输入数组进行排序。
Syntax:
句法:
rsort(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:
$arr1 = array(105, 108, 101, 100, 90);
Output:
arr1 (sorted)...
Array
(
[0] => 108
[1] => 105
[2] => 101
[3] => 100
[4] => 90
)
PHP code:
PHP代码:
<?php
$arr1 = array(105, 108, 101, 100, 90);
$arr2 = array("Amit", "Prem", "Prerana", "Aleesha", "Abhishek");
rsort($arr1);
print ("arr1 (sorted)...\n");
print_r ($arr1);
rsort($arr2);
print ("arr2 (sorted)...\n");
print_r ($arr2);
?>
Output
输出量
arr1 (sorted)...
Array
(
[0] => 108
[1] => 105
[2] => 101
[3] => 100
[4] => 90
)
arr2 (sorted)...
Array
(
[0] => Prerana
[1] => Prem
[2] => Amit
[3] => Aleesha
[4] => Abhishek
)
翻译自: https://www.includehelp.com/php/rsort-function-with-example.aspx
rsort和asort