SoFunction
Updated on 2025-03-10

Example explanation of array_multisort implementation of PHP multidimensional array sorting

array_multisort — sort multiple arrays or multidimensional arrays
illustrate
bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] )

array_multisort
(PHP 4, PHP 5)
Return TRUE if successful, and FALSE if failed.

array_multisort() can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one or more dimensions.

The string key name remains the same, but the numeric key name will be reindexed.

The input array is treated as a column of a table and sorted by rows - this is similar to the functionality of the ORDER BY clause of SQL. The first array is the main array to be sorted. If the rows (values) in the array are compared to 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 parameters can be an array or the sort flags listed below.

Sort order flags:
SORT_ASC – Sort in ascending order

SORT_DESC – Sort in descending order
Sort type flags:
SORT_REGULAR – Compare items according to the usual method

SORT_NUMERIC – Compare items by numerical value

SORT_STRING – Compare items by string
Two sort flags of the same kind cannot be specified after each array. The sort flag specified after each array is only valid for that array – before this, the default values ​​SORT_ASC and SORT_REGULAR.

Example 1. Sort multiple arrays

<?php
$ar1 = array(“10″, 100, 100, “a”);
$ar2 = array(1, 3, “2″, 1);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

In this example, after sorting, the first array will contain “10″,”a”,100,100. The second array will contain 1, 1, "2" and 3. The order of items in the second array is exactly the same as the order of the corresponding items in the first array (100 and 100).

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 multidimensional arrays

<?php
$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);
?>



In this example, after sorting, the first array will contain 10,100,100, "a" (sorted as a string), and the second array will contain 1,3, "2", 1 (sorted as a numerical value).

Example 3. Sorting multi-dimensional array

<?php
$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" (arranged as a string in ascending order). The second array will contain 1, 3, "2", 2, 1 (arranged 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. Sort the database results

In this example, each cell in the data array represents a row in a table. This is a typical data collection of database records.

The data in the examples are as follows:

volume | edition
——-+——–
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7

All data is stored in an array called data. This is usually the result obtained from the database by looping, such as mysql_fetch_assoc().

<?php
$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, volume will be arranged in descending order and edition will be arranged in ascending order.

Now there is an array containing rows, but array_multisort() requires an array containing columns, so use the following code to get the columns and then sort them.

<?php
// Get the list of columns
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}

// Order the data according to volume descending order and ascending order according to edition
// Sort by common keys as the last parameter
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

The data sets are 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 ordering

SORT_STRING and SORT_REGULAR are both case-sensitive, and uppercase letters are ranked before lowercase letters.

To sort case-insensitively, sort by lowercase letter 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 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

<?php
$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,
// Sort the score as a value from high to low
$grade["name"], SORT_STRING, SORT_ASC);
// Sort the name as a string 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 $grade array containing the score is sorted from high to low by score, while those with the same score are sorted from small to large by name. After sorting, Li Si was ranked 95 and Zhao Liu was ranked 60 and there was no objection to the fifth. Zhang San, Wang Wu and Liu Qi all have 70 points, and their rankings are arranged in alphabetical order of their names, Liu is in front, Wang is in back and Zhang is in the end. To distinguish, the three 70 points are represented by integers, floating point numbers and strings respectively, and the results of their sorting can be clearly seen in the program output.
Supplementary information:
The most complex sorting method for multi-dimensional array sorting in PHP language. In actual encoding, we will use the PHP function array_multisort() to achieve this complex sorting. For example, first sort a nested array using a normal keyword, and then sort it by another keyword. This is very similar to sorting multiple fields using SQL's ORDER BY statement.
PHP function asort() uses the specific method of value sorting
Detailed explanation of the functional characteristics of PHP function arsort()
Introduction to the characteristics of PHP natural language sorting
Specific implementation method of reverse order of PHP natural language
How to use PHP function usort() to implement custom sorting
The Listing J example specifically illustrates 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 an array of rows and columns in the $data array. Then, I use the PHP function array_multisort() to rearrange the data set, first sorting according to rating, and then sorting according to name if the rating is equal. Its output results are as follows:
Copy the codeThe code is as follows:

Array ([0] => Array
(
[id] => 2
[name] => Take That
[rating] => 1
) [1] => Array
(
[id] => 1
[name] => Boney M
[rating] => 3
)
[2] => Array
(
[id] => 4
[name] => Lusain
[rating] => 3
)
[3] => Array
(
[id] => 3
[name] => The Killers
[rating] => 4
)
)

The PHP function array_multisort() is one of the most useful functions in PHP and has a very wide range of applications. In addition, as you can see in the example, it can sort multiple unrelated arrays, use one of the elements as the basis for the next sort, and also sort the database result set.