Sorting array Values Besides I can use the various sorts of methods taught by the teacher, our PHP directly gives you a function rsort () function, the usage is very simple, but only suitable for the array ah.
Rsort (Array,sorttype)
Parameter description
Array required. The input array.
SortType is optional. Specifies how the values of the array are arranged. Possible values:
Sort_regular-Default. Processed in their original type (without changing the type).
Sort_numeric-Handle the value as a number
Sort_string-handles the value as a string
Sort_locale_string-handles the value as a string, based on local settings *.
The code is as follows |
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 ) |
Other references
The code is as follows |
Copy Code |
$data [] = Array (' volume ' = +, ' edition ' = 2); $data [] = Array (' volume ' = +, ' edition ' = 1); $data [] = Array (' volume ' = =, ' edition ' = 6); $data [] = Array (' volume ' = = 98, ' edition ' = 2); $data [] = Array (' volume ' = +, ' edition ' = 6); $data [] = Array (' volume ' = +, ' edition ' = 7); |
In this example, the volume is sorted in descending order, and the edition in ascending order.
Now that you have an array with rows, but Array_multisort () requires an array of columns, so use the following code to get the columns and sort them.
Get a list of columns
The code is as follows |
Copy Code |
foreach ($data as $key = = $row) { $volume [$key] = $row [' volume ']; $edition [$key] = $row [' Edition ']; } |
Sort data in descending order according to volume, in ascending order of edition
The $data as the last parameter, sorted by the Universal key
Array_multisort ($volume, Sort_desc, $edition, SORT_ASC, $data);
The data set is now sorted and the results are as follows:
The code is as follows |
Copy Code |
Volume | Edition -------+-------- 98 | 2 86 | 1 86 | 6 85 | 6 67 | 2 67 | 7 |
Actually there are many other ways to use it in the sort order, such as PHP's array of Arsort (), Asort (), Ksort (), Krsort (), Natsort (), Natcasesort (), Rsort (), Usort ( ), Array_multisort () and Uksort ().
http://www.bkjia.com/PHPjc/631308.html www.bkjia.com true http://www.bkjia.com/PHPjc/631308.html techarticle sorting array Values Besides I can use the various sorts of methods taught by the teacher, our PHP directly gives you a function rsort () function, the usage is very simple, but only suitable for the array ah. ...