Home > Article > Backend Development > Introduction to how to use array_multisort() in php_PHP tutorial
Use the array_multisort() function to sort multiple arrays or multi-dimensional arrays. Friends who are studying array sorting and multi-dimensional array sorting can take a look.
Function bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]]] )
Parameter description: The function sorts multiple arrays or multi-dimensional arrays
The first parameter is an array, each subsequent parameter may be an array or the following sort order flags
SORT_ASC - Default, sort in ascending order
SORT_DESC - Sort in descending order
Subsequently can Specify the type of sort
SORT_REGULAR - Default. Arrange each item in regular order.
SORT_NUMERIC - Sort each item in numerical order.
SORT_STRING - Sort each item in alphabetical order.
Example:
The result is:
$arr1
Array ( [0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100 )
# '10' is converted to the integer 10 when compared with 11, 100, 100, which is smaller than the other three numbers
# '10' is used as a string when compared with 'a', its first character '1' ascii code value is 49 which is 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 less than the other three Number
$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 at [0] position
# $arr1[2] => 100, $arr1[3] => 100 respectively correspond to $arr2 elements 3, '2'. 3 is greater than '2', so the $arr1[2] corresponding to 2 => 100 has the sorted subscript 3, and the $arr1[3] corresponding to 3 => 100 has the sorted index 4
Summary----------
1. The number of array elements involved in sorting remains the same
2. The positions of the sorted array elements correspond to, for example, '10' => 1 , 11 => 2
3. The following array is sorted based on the order of the previous array
4. If the previous array encounters equal elements, the following array is compared
';
//Original array format
$array = array(
'key1' => array(
'item1' => '65 ',
'item2' => '35',
'item3' => '84',
),
'key2' => array(
'item1' => '24',
),
'key3' => array(
'item1' => '38',
'item3' => '45',
),
);
//Keys to be sorted
//Sort according to item1 in the array
//You can also change to item2
$sort = 'item1' ;
foreach($array as $k => $v)
{
$newArr[$k] = $v[$sort];
}
//This function if If executed correctly, it will directly change the order of the original array key values
//If the execution fails, then it will return bool(false)
array_multisort($newArr,SORT_DESC, $array);
var_dump($array );
//---------------------The sorted array printing effect begins--------------- -----
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"
}
}
///---------------------The sorted array printing effect ends----- ----------------