Sort the array values. in addition to the various sorting methods taught by the instructor, our php will directly give you a function rsort (), which is very simple to use, but only suitable for arrays. Rsort (array
Sort the array values. in addition to the various sorting methods taught by the instructor, our php will directly give you a function rsort (), which is very simple to use, but only suitable for arrays.
Rsort (array, sorttype)
Parameter description
Array is required. Input array.
Sorttype (optional) specifies how to sort the values of an array. possible values:
SORT_REGULAR-by default, it is processed based on 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.
The 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
- )
For other references, the code is as follows:
- $ Data [] = array ('Volume '=> 67, 'version' => 2 );
- $ Data [] = array ('Volume '=> 86, 'version' => 1 );
- $ Data [] = array ('Volume '=> 85, 'version' => 6 );
- $ Data [] = array ('Volume '=> 98, 'version' => 2 );
- $ Data [] = array ('Volume '=> 86, 'version' => 6 );
- $ Data [] = array ('Volume '=> 67, 'version' => 7 );
In this example, we will sort volume in descending order and edition in ascending order. now we have an array containing rows, but array_multisort () needs an array containing columns, therefore, use the following code to retrieve and sort columns.
Get the list of columns, the code is as follows:
- Foreach ($ data as $ key => $ row ){
- $ Volume [$ key] = $ row ['Volume '];
- $ Edition [$ key] = $ row ['version'];
- }
- // Sort data by volume in descending order and edition in ascending order
- // Use $ data as the last parameter and sort it by a common key
- Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );
The data set is sorted as follows:
- Volume | edition
- ------- + --------
- 98 | 2
- 86 | 1
- 86 | 6
- 85 | 6
- 67 | 2
- 67 | 7
In fact, there are still many ways to use in sorting, such as the arsort (), asort (), ksort (), krsort (), natsort (), natcasesort (), rsort (), usort (), array_multisort (), and uksort ().