The array_multisort Function
The array_multisort
function allows you to sort several arrays simultaneously
or perform complex sorting of multidimensional arrays by one or more criteria.
Syntax
array_multisort(
array1,
[sorting_order = SORT_ASC],
[sorting_type = SORT_REGULAR],
array2,
array3,
...
);
sorting_order Parameter
The sorting_order
parameter sets
the sorting direction. Here are its values:
Parameter | Description |
---|---|
SORT_ASC |
Ascending order sort (default value). |
SORT_DESC |
Descending order sort. |
sorting_type Parameter
The sorting_type
parameter sets
the type of element comparison. Here are its values:
Parameter | Description |
---|---|
SORT_REGULAR |
Regular comparison of elements (default). |
SORT_NUMERIC |
Numeric comparison of elements. |
SORT_STRING |
String comparison of elements. |
SORT_LOCALE_STRING |
String comparison based on the current locale. |
SORT_NATURAL |
Natural order string sorting (like in a file explorer). |
SORT_FLAG_CASE |
Can be combined with SORT_STRING or SORT_NATURAL for case-insensitive sorting. |
Example
Sorting two arrays with specified parameters:
<?php
$numbers = [10, 2, 15, 4];
$letters = ['b', 'a', 'd', 'c'];
array_multisort(
$numbers, SORT_DESC, SORT_NUMERIC,
$letters, SORT_ASC, SORT_STRING
);
print_r($numbers);
print_r($letters);
?>
Code execution result:
[15, 10, 4, 2]
['d', 'b', 'c', 'a']
Example
Sorting a multidimensional array by a specified field:
<?php
$data = [
['name' => 'John', 'age' => 25],
['name' => 'Alice', 'age' => 22],
['name' => 'Bob', 'age' => 30]
];
$ages = array_column($data, 'age');
array_multisort($ages, SORT_ASC, $data);
print_r($data);
?>
Code execution result:
[
['name' => 'Alice', 'age' => 22],
['name' => 'John', 'age' => 25],
['name' => 'Bob', 'age' => 30]
]
Example
Sorting a multidimensional array by two specified fields:
<?php
$data = [
['name' => 'John', 'age' => 25],
['name' => 'alice', 'age' => 22],
['name' => 'Bob', 'age' => 30]
];
$names = array_column($data, 'name');
$ages = array_column($data, 'age');
array_multisort(
$names, SORT_ASC, SORT_STRING | SORT_FLAG_CASE,
$ages, SORT_DESC,
$data
);
print_r($data);
?>
Code execution result:
[
['name' => 'alice', 'age' => 22],
['name' => 'Bob', 'age' => 30],
['name' => 'John', 'age' => 25]
]
Example
Natural order string sorting:
<?php
$files = ['file1.txt', 'file10.txt', 'file2.txt'];
array_multisort($files, SORT_ASC, SORT_NATURAL);
print_r($files);
?>
Code execution result:
['file1.txt', 'file2.txt', 'file10.txt']
Example
Case-insensitive sorting:
<?php
$words = ['Apple', 'banana', 'cherry', 'apricot'];
array_multisort($words, SORT_ASC, SORT_STRING | SORT_FLAG_CASE);
print_r($words);
?>
Code execution result:
['Apple', 'apricot', 'banana', 'cherry']
See Also
-
the
sort
function,
which sorts by element values in ascending order -
the
rsort
function,
which sorts by element values in descending order -
the
ksort
function,
which sorts by keys in ascending order -
the
krsort
function,
which sorts by keys in descending order -
the
asort
function,
which sorts by element values in ascending order, preserving keys -
the
arsort
function,
which sorts by element values in descending order, preserving keys -
the
natsort
function,
which sorts using a natural order algorithm -
the
natcasesort
function,
which sorts using a natural order algorithm, case-insensitive -
the
usort
function,
which sorts using a user-defined comparison function -
the
uksort
function,
which sorts by keys using a user-defined comparison function -
the
uasort
function,
which sorts using a user-defined comparison function and preserves key association