array_multisort()
array_multisort() — Sort multiple or multi-dimensional arrays.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.
Signature
array_multisort(array &$array1, mixed $array1_sort_order = SORT_ASC, mixed $array1_sort_flags = SORT_REGULAR, mixed ...$rest): true
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| &$array1 | array | An array being sorted. |
| $array1_sort_order | mixed | The order used to sort the previous array argument. Either SORT_ASC to sort ascendingly or SORT_DESC to sort descendingly. |
| $array1_sort_flags | mixed | Sort options for the previous array argument: |
| ...$rest | mixed | More arrays, optionally followed by sort order and flags. Only elements corresponding to equivalent elements in previous arrays are compared. In other words, the sort is lexicographical. |
Return value
Always returns true.
Example
A runnable snippet:
$ar1 = array(10, 100, 100, 0); $ar2 = array(1, 3, 2, 4); array_multisort($ar1, $ar2); var_dump($ar1); var_dump($ar2);
Output:
array(4) { [0]=> int(0) [1]=> int(10) [2]=> int(100) [3]=> int(100) } array(4) { [0]=> int(4) [1]=> int(1) [2]=> int(2) [3]=> int(3) }
Changelog
| Version | Change |
|---|---|
| 8.5.0 | The return type is true now; previously, it was bool. |
| 8.0.0 | Now throws a ValueError or TypeError for invalid arguments. Previously, an E_WARNING was raised and false was returned. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_multisort() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.