HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_multisort

Description

The array_multisort of Array for PHP 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.

ConstantDescription
SORT_ASCsort ascending
SORT_DESCsort 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.

ConstantDescription
SORT_REGULARcompare items normally (don't change types)
SORT_NUMERICcompare items numerically
SORT_STRINGcompare items as strings
SORT_LOCALE_STRINGcompare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
SORT_NATURALcompare items as strings using "natural ordering" like natsort()
SORT_FLAG_CASEcan 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 = ["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 = ["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 = ["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 = ["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 = ["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 = ["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 = ["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 = ["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 = ["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 = ["b", "B", "c", "A", "a", "C"];
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_order = SORT_ASC;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_order = SORT_DESC;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_flags = SORT_REGULAR;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_flags = SORT_NUMERIC;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_flags = SORT_STRING;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_flags = SORT_LOCALE_STRING;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_flags = SORT_NATURAL;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];
$array1_sort_flags = SORT_NATURAL | SORT_FLAG_CASE;
$array2 = [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 = ["b", "B", "c", "A", "a", "C"];

$return = array_multisort($array1);

var_export($return);

?>
true

20 · array_column

<?

$array =
[
    ['volume' => 67, 'edition' => 2],
    ['volume' => 86, 'edition' => 1],
    ['volume' => 85, 'edition' => 6],
    ['volume' => 98, 'edition' => 2],
    ['volume' => 86, 'edition' => 6],
    ['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 = ['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
)