SoFunction
Updated on 2025-04-07

Introduction to the use of array_multisort in php

Functions that use the array_multisort() function to sort multiple arrays or multidimensional arrays. Those who are studying array sorting and multidimensional array sorting can take a look.

Function bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]]] )

Parameter description: Function sorts multiple arrays or multi-dimensional arrays

The first parameter is an array, and each subsequent parameter may be an array, or the following sort order flag
SORT_ASC - default, sorted in ascending order
SORT_DESC - sort in descending order
You can then specify the sorting type
SORT_REGULAR - Default. Arrange each item in a regular order.
SORT_NUMERIC - Order each item in numerical order.
SORT_STRING - Order each item alphabetically.
example:

Copy the codeThe code is as follows:

<?php
$arr1 = array('10', 11, 100, 100, 'a');
$arr2 = array(1, 2, 3, '2', 5);
array_multisort($arr1, $arr2);
?>

The result is:
$arr1
Array ( [0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100 )
# '10' is converted to integer 10 when compared with 11, 100, 100, smaller than the other three numbers
# '10' is used as a string when compared with 'a', and its first character '1'ascii code value is 49 less than 'a' (ascii value is 97), so '10' is the smallest element
# 'a' is converted to an integer 0 when comparing the other three numbers, which is smaller than the other three numbers.
$arr2
Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3 )
# $arr2 element 1 corresponds to $arr1 element '10' position, so it is ranked in the [0] position
# $arr1[2] => 100, $arr1[3] => 100 corresponds to $arr2 elements 3, '2', respectively. 3 is greater than '2', so the $arr1[2] corresponding to 2 => The subscript after sorting of 100 is 3, and the subscript for 3 is $arr1[3] corresponding to 3 => The subscript for sorting of 100 is 4
Summarize----------
1. The number of elements participating in the sorting remains the same
2. The corresponding positions of the elements of the sorting array are as follows: ‘10’ => 1, 11 => 2
3. The following arrays are sorted based on the order of the previous arrays
4. If the previous array encounters an equal element, compare the subsequent array.



Below are the examples used. The examples in the manual are not listed, just a few of them in my own work.
The most common ones I encounter in my work may be two-dimensional arrays. I want to try three-dimensional arrays, but I still forget it.
Copy the codeThe code is as follows:

header('Content-Type: text/html; charset=utf-8');
echo '<pre>';
//Original array format
$array = array(
'key1' => array(
'item1' => '65',
'item2' => '35',
'item3' => '84',
),
'key2' => array(
'item1' => '24',
),
'key3' => array(
'item1' => '38',
'item3' => '45',
),
);
//The key to sort
//Sort by item1 in the array
//You can also change to item2
$sort = 'item1';
foreach($array as $k => $v)
{
$newArr[$k] = $v[$sort];
}
//If this function is executed correctly, it will directly change the order of the original array key value
//If the execution fails, then it will return bool(false)
array_multisort($newArr,SORT_DESC, $array);
var_dump($array);
//-----------------------------------------------------------------------------------------------------------------------------
array(3) {
["key1"]=>
array(3) {
["item1"]=>
string(2) "65"
["item2"]=>
string(2) "35"
["item3"]=>
string(2) "84"
}
["key3"]=>
array(2) {
["item1"]=>
string(2) "38"
["item3"]=>
string(2) "45"
}
["key2"]=>
array(1) {
["item1"]=>
string(2) "24"
}
}
//-----------------------------------------------------------------------------------------------------------------------------