array_multisort

Sort multiple or multi-dimensional arrays

Syntax

array_multisort(array &$array1, mixed $array1_sort_order = SORT_ASC, mixed $array1_sort_flags = SORT_REGULAR, mixed ...$rest): bool

Parameters

array1

An array being sorted.

array1_sort_order

The order used to sort the previous array argument.

Constant Description
SORT_ASC sort ascending
SORT_DESC sort descending

This argument can be swapped with array1_sort_flags or omitted entirely, in which case SORT_ASC is assumed.

array1_sort_flags

The sort options for the previous array argument.

Constant Description
SORT_REGULAR compare items normally (don't change types)
SORT_NUMERIC compare items numerically
SORT_STRING compare items as strings
SORT_LOCALE_STRING compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
SORT_NATURAL compare items as strings using "natural ordering" like natsort()
SORT_FLAG_CASE can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

This argument can be swapped with array1_sort_order or omitted entirely, in which case SORT_REGULAR is assumed.

rest

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

Returns TRUE on success or FALSE on failure.

Examples

array1

<?

$array1 = array("b", "B", "c", "A", "a", "C");

array_multisort($array1);

print_r($array1);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)

array1_sort_order | SORT_ASC

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_order = SORT_ASC;

array_multisort($array1, $array1_sort_order);

print_r($array1);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)

array1_sort_order | SORT_DESC

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_order = SORT_DESC;

array_multisort($array1, $array1_sort_order);

print_r($array1);

?>
Array
(
    [0] => c
    [1] => b
    [2] => a
    [3] => C
    [4] => B
    [5] => A
)

array1_sort_flags | SORT_REGULAR

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_REGULAR;

array_multisort($array1, $array1_sort_flags);

print_r($array1);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)

array1_sort_flags | SORT_NUMERIC

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_NUMERIC;

array_multisort($array1, $array1_sort_flags);

print_r($array1);

?>
Array
(
    [0] => b
    [1] => B
    [2] => c
    [3] => A
    [4] => a
    [5] => C
)

array1_sort_flags | SORT_STRING

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_STRING;

array_multisort($array1, $array1_sort_flags);

print_r($array1);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)

array1_sort_flags | SORT_LOCALE_STRING

<?

$category = LC_ALL;
$locale = "en_US";

setlocale($category, $locale);

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_LOCALE_STRING;

array_multisort($array1, $array1_sort_flags);

print_r($array1);

?>
Array
(
    [0] => a
    [1] => A
    [2] => b
    [3] => B
    [4] => c
    [5] => C
)

array1_sort_flags | SORT_NATURAL

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_NATURAL;

array_multisort($array1, $array1_sort_flags);

print_r($array1);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)

array1_sort_flags | SORT_FLAG_CASE

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_NATURAL | SORT_FLAG_CASE;

array_multisort($array1, $array1_sort_flags);

print_r($array1);

?>
Array
(
    [0] => A
    [1] => a
    [2] => b
    [3] => B
    [4] => c
    [5] => C
)

array2

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array2 = array(0, 1, 2, 3, 4, 5);

array_multisort($array1, $array2);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 5
    [3] => 4
    [4] => 0
    [5] => 2
)

array2_sort_order | SORT_ASC

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_order = SORT_ASC;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_order = SORT_ASC;

array_multisort($array1, $array1_sort_order, $array2, $array2_sort_order);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 5
    [3] => 4
    [4] => 0
    [5] => 2
)

array2_sort_order | SORT_DESC

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_order = SORT_DESC;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_order = SORT_DESC;

array_multisort($array1, $array1_sort_order, $array2, $array2_sort_order);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => c
    [1] => b
    [2] => a
    [3] => C
    [4] => B
    [5] => A
)
Array
(
    [0] => 2
    [1] => 0
    [2] => 4
    [3] => 5
    [4] => 1
    [5] => 3
)

array2_sort_flags | SORT_REGULAR

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_REGULAR;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_flags = SORT_REGULAR;

array_multisort($array1, $array1_sort_flags, $array2, $array2_sort_flags);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 5
    [3] => 4
    [4] => 0
    [5] => 2
)

array2_sort_flags | SORT_NUMERIC

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_NUMERIC;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_flags = SORT_NUMERIC;

array_multisort($array1, $array1_sort_flags, $array2, $array2_sort_flags);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => b
    [1] => B
    [2] => c
    [3] => A
    [4] => a
    [5] => C
)
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)

array2_sort_flags | SORT_STRING

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_STRING;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_flags = SORT_STRING;

array_multisort($array1, $array1_sort_flags, $array2, $array2_sort_flags);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 5
    [3] => 4
    [4] => 0
    [5] => 2
)

array2_sort_flags | SORT_LOCALE_STRING

<?

$category = LC_ALL;
$locale = "en_US";

setlocale($category, $locale);

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_LOCALE_STRING;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_flags = SORT_LOCALE_STRING;

array_multisort($array1, $array1_sort_flags, $array2, $array2_sort_flags);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => a
    [1] => A
    [2] => b
    [3] => B
    [4] => c
    [5] => C
)
Array
(
    [0] => 4
    [1] => 3
    [2] => 0
    [3] => 1
    [4] => 2
    [5] => 5
)

array2_sort_flags | SORT_NATURAL

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_NATURAL;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_flags = SORT_NATURAL;

array_multisort($array1, $array1_sort_flags, $array2, $array2_sort_flags);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => a
    [4] => b
    [5] => c
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 5
    [3] => 4
    [4] => 0
    [5] => 2
)

array2_sort_flags | SORT_FLAG_CASE

<?

$array1 = array("b", "B", "c", "A", "a", "C");
$array1_sort_flags = SORT_NATURAL | SORT_FLAG_CASE;
$array2 = array(0, 1, 2, 3, 4, 5);
$array2_sort_flags = SORT_NATURAL | SORT_FLAG_CASE;

array_multisort($array1, $array1_sort_flags, $array2, $array2_sort_flags);

print_r($array1);
print_r($array2);

?>
Array
(
    [0] => A
    [1] => a
    [2] => b
    [3] => B
    [4] => c
    [5] => C
)
Array
(
    [0] => 3
    [1] => 4
    [2] => 0
    [3] => 1
    [4] => 2
    [5] => 5
)

Return

<?

$array1 = array("b", "B", "c", "A", "a", "C");

$return = array_multisort($array1);

var_export($return);

?>
true

array_column

<?

$array = array(
    array('volume' => 67, 'edition' => 2),
    array('volume' => 86, 'edition' => 1),
    array('volume' => 85, 'edition' => 6),
    array('volume' => 98, 'edition' => 2),
    array('volume' => 86, 'edition' => 6),
    array('volume' => 67, 'edition' => 7)
);

$array1 = array_column($array, 'volume');
$array1_sort_order = SORT_DESC;
$array2 = array_column($array, 'edition');
$array2_sort_order = SORT_ASC;

// add $array as last parameter to sort by common key
array_multisort($array1, $array1_sort_order, $array2, $array2_sort_order, $array);

print_r($array);

?>
Array
(
    [0] => Array
        (
            [volume] => 98
            [edition] => 2
        )

    [1] => Array
        (
            [volume] => 86
            [edition] => 1
        )

    [2] => Array
        (
            [volume] => 86
            [edition] => 6
        )

    [3] => Array
        (
            [volume] => 85
            [edition] => 6
        )

    [4] => Array
        (
            [volume] => 67
            [edition] => 2
        )

    [5] => Array
        (
            [volume] => 67
            [edition] => 7
        )

)

array_map

<?

$callback = 'strtolower';
$array = array('Alpha', 'atomic', 'Beta', 'bank');

$array1 = array_map($callback, $array);
$array1_sort_flags = SORT_STRING;

array_multisort($array1, $array1_sort_flags, $array);

print_r($array);

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

PHP

Home Menu