This article briefly explains how PHP uses its own functions to sort the element values of an array in descending order. Please refer to it if necessary.
The rsort() function sorts the elements of an array in reverse order by key value. Basically the same function as arsort().
Note: This function assigns new key names to cells in array. This will delete the original keys rather than just reorder them.
Returns TRUE if successful, otherwise returns FALSE.
The optional second parameter contains additional sorting flags.
Grammar
rsort(array,sorttype) parameter description
array required. The input array.
sorttype optional. Specifies how to arrange the values of an array. Possible values:
SORT_REGULAR - Default. Processed with their original type (without changing type).
SORT_NUMERIC - treat values as numbers
SORT_STRING - handle values as strings
SORT_LOCALE_STRING - Handle values as strings, based on local settings*.
*: This value is newly added in PHP 4.4.0 and 5.0.2. Prior to PHP 6, the system locale was used, which could be changed with setlocale(). Since PHP 6, the i18n_loc_set_default() function must be used.
Example
The code is as follows
代码如下 |
复制代码 |
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
rsort($my_array);
print_r($my_array);
?>输出:
Array
(
[0] => Horse
[1] => Dog
[2] => Cat
)
Like sort(), rsort() assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys. This means that it will destroy associative keys.
$animals = array("dog"=>"large", "cat"=>"medium", "mouse"=>"small");
print_r($animals);
//Array ( [dog] => large [cat] => medium [mouse] => small )
rsort($animals);
print_r($animals);
//Array ( [0] => small [1] => medium [2] => large )
Use KSORT() or KRSORT() to preserve associative keys.
|
|
Copy code |
|
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
rsort($my_array);
print_r($my_array);
?>Output:
Array
(
[0] => Horse
[1] => Dog
[2] => Cat
)
Like sort(), rsort() assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys. This means that it will destroy associative keys.
$animals = array("dog"=>"large", "cat"=>"medium", "mouse"=>"small");
print_r($animals);
//Array ( [dog] => large [cat] => medium [mouse] => small )
rsort($animals);
//Array ( [0] => small [1] => medium [2] => large )
Use KSORT() or KRSORT() to preserve associative keys.
http://www.bkjia.com/PHPjc/629259.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629259.htmlTechArticleThis article briefly explains how PHP uses its own function to sort the element values of the array in descending order. If necessary for reference. The rsort() function sorts the elements of the array according to key value...