This article briefly introduces how php uses its own functions to sort the element values of arrays in descending order. For more information, see rsort () the function sorts the elements of the array in reverse order by key value, which is similar to the function of arsort ().
This article briefly explains how php uses its own functions to sort the element values of arrays in descending order. For more information, see.
The rsort () function sorts array elements in reverse order based on the key value. This function is basically the same as that of arsort.
Annotation; this function assigns a new key name to the unit in array. This will delete the original key name, not just the reorder.
TRUE is returned if the operation is successful. otherwise, FALSE is returned.
The second optional parameter contains another sorting flag.
Syntax
Rsort (array, sorttype) parameter description
Array required. input array.
Sorttype is optional. specifies how to arrange the values of the array. possible values;
SORT_REGULAR-default. process with their original type (without changing the type ).
SORT_NUMERIC-process the value as a number
SORT_STRING-process the value as a string
SORT_LOCALE_STRING-process the value as a string based on local settings *.
*; This value is newly added to PHP 4.4.0 and 5.0.2. before PHP 6, you can use setlocale () to change the region settings of the system. from PHP 6, the i18n_loc_set_default () function must be used.
The instance code is as follows:
-
- $ 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 );
- Print_r ($ animals );
- // Array ([0] => small [1] => medium [2] => large)
- Use KSORT () or KRSORT () to preserve associative keys.