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

1 · 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
)

2 · 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
)

3 · 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
)

4 · 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
)

5 · 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
)

6 · 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
)

7 · 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
)

8 · 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
)

9 · 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
)

10 · 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
)

11 · 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
)

12 · 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
)

13 · 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
)

14 · 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
)

15 · 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
)

16 · 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
)

17 · 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
)

18 · 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
)

19 · Return

<?

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

$return = array_multisort($array1);

var_export($return);

?>
true

20 · 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
        )

)

21 · 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
)

array

array_change_key_case

array_chunk

array_column

array_combine

array_count_values

array_diff

array_diff_assoc

array_diff_key

array_diff_uassoc

array_diff_ukey

array_fill

array_fill_keys

array_filter

array_flip

array_intersect

array_intersect_assoc

array_intersect_key

array_intersect_uassoc

array_intersect_ukey

array_key_exists

array_key_first

array_key_last

array_keys

array_map

array_merge

array_merge_recursive

array_pad

array_pop

array_product

array_push

array_rand

array_reduce

array_replace

array_replace_recursive

array_reverse

array_search

array_shift

array_slice

array_splice

array_sum

array_udiff

array_udiff_assoc

array_udiff_uassoc

array_uintersect

array_uintersect_assoc

array_uintersect_uassoc

array_unique

array_unshift

array_values

array_walk

array_walk_recursive

arsort

asort

compact

count

current

end

extract

in_array

key

key_exists

krsort

ksort

list

natcasesort

natsort

next

pos

prev

range

reset

rsort

shuffle

sizeof

sort

uasort

uksort

usort

PHP

Home Menu