Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.
array_multisort-to sort multiple arrays or multidimensional arrays
Description
BOOL Array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]] )
Array_multisort
(PHP 4, PHP 5)
Returns TRUE if successful, and FALSE if it fails.
Array_multisort () can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.
The association (string) key name remains the same, but the numeric key name is re-indexed.
The input array is treated as a column of a table and sorted in rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. The rows (values) in the array are compared to the same words, sorted by the size of the corresponding values in the next input array, and so on.
The parameter structure of this function is somewhat unusual, but very flexible. The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.
Sort order Flags:
sort_asc– Sort by ascending order
sort_desc– Sorted in descending order
Sort Type flag:
sort_regular– Compare projects by usual method
sort_numeric– the project by numerical comparison
sort_string– to compare items by string
You cannot specify two of the same sort flags after each array. The sort flags specified after each array are valid only for that array-the default values SORT_ASC and Sort_regular before.
Example 1. To sort multiple arrays
<?php $ar 1 = Array ("10″, +, +," a "); $ar 2 = Array (1, 3, "2″, 1); Array_multisort ($ar 1, $ar 2); Var_dump ($ar 1); Var_dump ($ar 2);?>
In this example, after sorting, the first array will contain "10″," a ", 100,100. The second array will contain 1, 1, "2″,3." The order of the items in the second array is exactly the same as the corresponding items (100 and 100) in the first array.
Array (4) {[0]=> string (2) "10″[1]=> string (1)" a "[2]=> int (+) [3]=> int (+)} array (4) {[0]=> int (1) [1]=> Int (1) [2]=> string (1) "2″[3]=> int (3)}
Example 2. To sort a multidimensional array
<?php $ar = Array (Array ("10″,", "a"), Array (1, 3, "2″, 1)"); Array_multisort ($ar [0], SORT_ASC, sort_string, $ar [1], sort_numeric, SORT_DESC);?>
In this example, after sorting, the first array will contain 10,100,100, "a" (ascending sort as a string), and the second array will contain 1, 3, "2″,1 (sorted as numeric descent).
Example 3. Sorting multi-dimensional array
<?php $ar = Array ("10″, One, one, 3," a "), Array (1, 2," 2″, 1)); Array_multisort ($ar [0], SORT_ASC, sort_string, $ar [1], sort_numeric, SORT_DESC); Var_dump ($ar);?>
In this example, after sorting, the first array becomes "10″,100,100,11," a "(as strings are sorted in ascending order). The second array will contain 1, 3, "2″, 2, 1 (as a number in descending order).
Array (2) {[0]=> Array (5) {[0]=> string (2) "10″[1]=> int" [2]=> int (+) [3]=> int (one) [4]=> Strin G (1) "A"} [1]=> Array (5) {[0]=> int (1) [1]=> int (3) [2]=> string (1) "2″[3]=> int (2) [4]=> int (1)}}
Example 4. To sort the results of a database
In this example, each cell in the data array represents a row in a table. This is a data collection of typical database records.
The data in the example is as follows:
Volume | Edition
——-+--–
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7
The data is all stored in an array named data. This is usually done by looping the results from the database, such as MYSQL_FETCH_ASSOC ().
<?php $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.
<?php//Get the list of columns foreach ($data as $key = + $row) {$volume [$key] = $row [' volume ']; $edition [$key] = $row [' Edition ']; }//volume The data in descending order according to the edition ascending order//$data as the last parameter, sort the array_multisort with the Universal key ($volume, Sort_desc, $edition, SORT_ASC, $DATA);?>
The data set is now sorted and the results are as follows:
Volume | Edition
——-+--–
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
Example 5. Case-insensitive alphabetical sorting
Sort_string and Sort_regular are both case-sensitive letters, and uppercase letters precede lowercase letters.
To make a case-insensitive sort, sort by a lowercase copy of the original array.
<?php $array = Array (' Alpha ', ' Atomic ', ' Beta ', ' Bank '); $array _lowercase = Array_map (' Strtolower ', $array); Array_multisort ($array _lowercase, SORT_ASC, sort_string, $array); Print_r ($array);?>
The example above will output:
Array ([0] = Alpha [1] = atomic [2] = = Bank [3] = = Beta)
Supplemental Information:
is the most complex sort of a multidimensional array in the PHP language. We will use PHP function Array_multisort () in the actual code to achieve this complex sort. For example, first sort a nested array with a common keyword, and then sort by another keyword. This is similar to sorting multiple fields with an order BY statement that uses SQL.
PHP function Asort () Parse
PHP function Arsort () in the specific way of sorting the function features of the
PHP natural language sorting
How to implement PHP natural language reverse order
How to use PHP functions Usort () Implementing a custom sort
Listing J example shows us how the PHP function Array_multisort () works:
1, "name" = "Boney M", "rating" and "= 3", Array ("id" = > 2, "name" = "Take That", "rating" and "= 1", Array ("id" = 3, "name" = "The Killers", "rating" + 4), AR Ray ("id" = 4, "name" = "Lusain", "rating" = 3),); foreach ($data as $key + $value) {$name [$key] = $value [name]; $rating [$key] = $value [rating];} array_multisort ($rati Ng, $name, $data); Print_r ($data);? > Here, we simulate a row and column array in the $DATA array. I then rearrange the data collection using the PHP function Array_multisort (), starting with rating and then sorting by name if the rating is equal. It outputs the following result:
Array ([0] = = Array ([id] = 2 [name] = = [rating] + 1) [1] = = Array ([id] = 1 [name] => ; Boney M [rating] + 3) [2] = = Array ([id] = 4 [name] + lusain [rating] = 3) [3] = = Array ([ID] =& Gt 3 [Name] = The Killers [rating] = 4))
php function Array_multisort () is one of the most useful functions in PHP, and it has a very wide range of applications. Also, as you can see in the example, it is possible to sort multiple unrelated arrays, use one of them as the basis for the next sort, and sort the database result set.