array_multisort — 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 failed.
array_multisort() can be used to sort multiple arrays at once, or to sort multi-dimensional arrays according to one or more dimensions.
Associative (string) key names remain unchanged, but numeric key names will be re-indexed.
The input array is treated as a table column and sorted by row - this is similar to the functionality of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on.
The parameter structure of this function is somewhat unusual, but very flexible. The first parameter must be an array. Each of the following arguments can be an array or a sort flag listed below.
Sort order flags:
SORT_ASC - Sort in ascending order
SORT_DESC - Sort in descending order
Sort type flags:
SORT_REGULAR - Compare items in the usual way
SORT_NUMERIC - Compare items based on numeric values
SORT_STRING - Compare items based on strings
Two similar sorting flags cannot be specified after each array. The sort flags specified after each array are valid only for that array – before that the default values SORT_ASC and SORT_REGULAR were used.
Example 1. Sort multiple arrays
$ar1 = array(“10″, 100, 100, “a”);
$ar2 = array(1, 3, “2″, 1);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
After sorting in this example, 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 order of the corresponding items (100 and 100) in the first array.
array(4) {
[0]=> string(2) “10″
[1]=> string(1) “a”
[2]= > int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(1)
[1] => int(1)
[2]=> string(1) “2″
[3]=> int(3)
}
Example 2. Sort multi-dimensional array
$ar = array (array (“10″, 100, 100, “a”), array (1, 3, “2 ″, 1));
array_multisort ($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
?>
After sorting in this example, the first array will contain 10, 100, 100, "a" (as ascending order of strings), and the second array will contain 1, 3, "2", 1 (as numerically descending order).
Example 3. Sorting multi-dimensional array
$ar = array(
array(“10″, 11, 100, 100, “a” ),
array( 1, 2, “2″, 3, 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 will become "10", 100, 100, 11, "a ” (treated as a string in ascending order). The second array will contain 1, 3, “2″, 2, 1 (treated as numbers in descending order).
array(2) {
[0]=> array(5) {
[0]=> string(2) “10″
[1]=> int(100)
[2]=> int(100)
[3]=> int(11)
[4]=> string(1) “a”
}
[1]=> array(5) {
[0]=> int(1)
[1]=> int(3)
[2]=> string (1) “2″
[3]=> int(2)
[4]=> int(1)
}
}
Example 4. Sorting database results
In this example, each cell in the data array represents a row in a table. This is a typical collection of data recorded in a database.
The data in the example is as follows:
volume | edition
——-+——–
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7
The data are all stored in the array named data. This is usually obtained from the database through a loop, such as mysql_fetch_assoc().
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array ('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data [] = array('volume' => 67, 'edition' => 7);
?>
In this example, the volume will be sorted in descending order and the edition will be sorted in ascending order.
Now you have an array with rows, but array_multisort() requires an array with columns, so use the following code to get the columns and then sort them.
// Get the list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row[' volume'];
$edition[$key] = $row['edition'];
}
// Sort the data in descending order according to volume and in ascending order according to edition
// Sort by common key with $data as last parameter
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>
The data collection is now sorted , the results are as follows:
volume | edition
——-+——–
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
Example 5. Case-insensitive sorting
SORT_STRING and SORT_REGULAR are both case-sensitive, and uppercase letters will Comes before lowercase letters.
To perform case-insensitive sorting, sort by lowercase letters of the original array.
$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 above example will output:
Array
(
[0] => Alpha
[1] => atomic
[2] => bank
[3] => Beta
)
[Translator’s Note] This function is quite useful. To help you understand, please look at the following example:
Example 6. Ranking
$grade = array(“score” => array(70, 95, 70.0, 60, “70″),
“name” => array( "Zhang San", "Li Si", "Wang Wu",
"Zhao Liu", "Liu Qi"));
array_multisort($grade["score"], SORT_NUMERIC, SORT_DESC,
// Use scores as numerical values, sort from high to low
$grade["name"], SORT_STRING, SORT_ASC);
// Use names as strings, sort from small to large
var_dump($ grade);
?>
The above example will output:
array(2) {
["score"]=>
array(5) {
[0]=>
int(95)
[1]=>
string(2) “70″
[2]=>
float(70 )
[3]=>
int(70)
[4]=>
int(60)
}
["name"]=>
array(5) {
[0]=>
string(5) “Li Si”
[1]=>
string(6) “Liu Qi”
[ 2]=>
string(7) “Wang Wu”
[3]=>
string(9) “Zhang San”
[4]=>
string( 8) “Zhao Liu”
}
}
In this example, the array $grade containing grades is sorted from high to low by score (score), and people with the same score are sorted by name ( name) in ascending order. After sorting, Li Si ranked first with 95 points, and Zhao Liu ranked fifth with 60 points. There is no objection. Zhang San, Wang Wu and Liu Qi all scored 70 points, and their rankings were arranged alphabetically by their names, with Liu first, Wang second and Zhang last. For the sake of distinction, the three 70 points are represented by integers, floating point numbers and strings respectively, and their sorted results can be clearly seen in the program output.
Supplementary information:
The most complicated sorting method for multi-dimensional array sorting in PHP language. We will use the PHP function array_multisort() in actual coding to implement this complex sorting. For example, a nested array is first sorted using a common key and then sorted based on another key. This is very similar to using SQL's ORDER BY statement to sort multiple fields.
The PHP function asort() analyzes the specific way of sorting by value
Detailed explanation of the functional characteristics of the PHP function asort()
Introduction to the characteristics of PHP natural language sorting
The specific implementation method of PHP natural language reverse order
How to use the PHP function usort() to implement custom sorting
The Listing J example explains for us how the PHP function array_multisort() works:
1, "name" => "Boney M", "rating " => 3), array("id" => 2, "name" => "Take That", "rating" => 1), array("id" => 3, "name" => "The Killers", "rating" => 4), array("id" => 4, "name" => "Lusain", "rating" => 3), ); foreach ( $data as $key => $value) { $name[$key] = $value[name]; $rating[$key] = $value[rating]; } array_multisort($rating, $name, $data) ; print_r($data);?> Here, we simulate a row and column array in the $data array. I then use the PHP function array_multisort() to reorder the data set, first by rating, and then, if the ratings are equal, by name. Its output is as follows: