PHP Function Reference

PHP array_multisort() Function



The PHP array_multisort() function is used to sort multiple arrays at once, or a multi-dimensional array by one or more dimensions. Note that the string keys will be maintained, but numeric keys will be re-indexed by this function.

Syntax

array_multisort(array1, array1_sort_order, array1_sort_flags, rest)

Parameters

array1 Required. Specify an array being sorted.
array1_sort_order Required. Specify the sorting order. Possible values are:
  • SORT_ASC - Default. Sort in ascending order.
  • SORT_DESC - Sort in descending order.
This argument can be swapped with array1_sort_flags or omitted entirely.
array1_sort_flags Optional. Specify sorting type flags to modify the sorting behavior. Sorting type flags are:
  • SORT_REGULAR - Default. Compare items normally.
  • SORT_NUMERIC - Compare items numerically.
  • SORT_STRING - Compare items as strings.
  • SORT_LOCALE_STRING - Compare items as strings, based on current locale.
  • SORT_NATURAL - Compare items as strings using natural ordering.
  • SORT_FLAG_CASE - Compare items as strings and case-insensitive. Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL.
This argument can be swapped with array1_sort_order or omitted entirely.
rest Optional. Specify 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

Returns true on success or false on failure.

Example: sorting multiple arrays

In the example below, after sorting the first array, the entries in the second array corresponding to the identical entries in the first array (100 and 100) are sorted as well.

<?php
$arr1 = array(10, 100, 100, 0);
$arr2 = array(1, 3, 2, 4);
array_multisort($arr1, $arr2);

print_r($arr1);
print_r($arr2);
?>

The output of the above code will be:

Array
(
    [0] => 0
    [1] => 10
    [2] => 100
    [3] => 100
)
Array
(
    [0] => 4
    [1] => 1
    [2] => 2
    [3] => 3
)

Example: sorting multi-dimensional array

Consider the example below where after sorting the first array, the entries in the second array corresponding to the identical entries in the first array (500 and 500) are sorted in descending order.

<?php
$arr = array(
  array("10", 11, 500, 500, "a"),
  array(   1,  2, "2",   3,   1)
);
array_multisort($arr[0], SORT_ASC, SORT_STRING,
                $arr[1], SORT_NUMERIC, SORT_DESC);

print_r($arr);
?>

The output of the above code will be:

Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 500
            [2] => 500
            [3] => 11
            [4] => a
        )

    [1] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 2
            [3] => 2
            [4] => 1
        )

)

Example: case insensitive sorting

Both SORT_STRING and SORT_REGULAR are case sensitive, strings starting with a capital letter will come before strings starting with a lowercase letter. To perform a case insensitive sort, forcing the sorting order to be determined by a lowercase copy of the original array can be used.

<?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 output of the above code will be:

Array
(
    [0] => Alpha
    [1] => atomic
    [2] => bank
    [3] => Beta
)

❮ PHP Array Reference