W3docs

Array Multisort in PHP: A Comprehensive Guide

PHP provides a wide range of functions for web developers. Learn how array_multisort() sorts parallel arrays and multiple columns at once.

array_multisort() sorts one or more arrays at once. Its real power is sorting parallel arrays — several arrays that represent columns of the same dataset — so that when you reorder one, the others move in lockstep to keep matching rows aligned. This page covers the syntax, multi-column (tie-breaking) sorts, the keys-reindexing gotcha, and how it differs from sort() and usort().

Syntax

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

Arguments are read in groups: each array may be followed by an optional sort-order constant and an optional sort-flags constant. The function sorts in place (the arrays are passed by reference) and returns true on success, false on failure.

ConstantEffect
SORT_ASCSort ascending (default)
SORT_DESCSort descending
SORT_REGULARCompare items normally (default)
SORT_NUMERICCompare items as numbers
SORT_STRINGCompare items as strings

Sorting a single array

In its simplest form it behaves like sort() — it orders one array in place:

$data = [3, 1, 2];
array_multisort($data);
print_r($data);

Output:

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

Sorting parallel arrays (the main use case)

Suppose you have two arrays that line up by index — one of ages, one of names — and you want to sort by age while keeping each name attached to its age. Pass both arrays; the first array drives the order and every other array is rearranged the same way:

$ages  = [25, 25, 30];
$names = ['John', 'Jane', 'Alice'];

array_multisort($ages, SORT_ASC, $names, SORT_ASC);

print_r($ages);
print_r($names);

Output:

Array
(
    [0] => 25
    [1] => 25
    [2] => 30
)
Array
(
    [0] => Jane
    [1] => John
    [2] => Alice
)

Notice the tie-break: both 25-year-olds keep their slots, but because a second array ($names, SORT_ASC) was supplied, the rows with equal ages are ordered by name — Jane before John.

Sorting by multiple criteria

To break ties, list arrays in priority order: the first array is the primary sort key, the next breaks ties in the first, and so on. Each array gets its own order flag, so you can mix ascending and descending columns:

$volume  = [67, 86, 85, 98, 86, 67];
$edition = [2, 1, 6, 2, 6, 7];

// Sort by volume DESC, then by edition ASC for equal volumes.
array_multisort($volume, SORT_DESC, $edition, SORT_ASC);

print_r($volume);
print_r($edition);

Output:

Array
(
    [0] => 98
    [1] => 86
    [2] => 86
    [3] => 85
    [4] => 67
    [5] => 67
)
Array
(
    [0] => 2
    [1] => 1
    [2] => 6
    [3] => 6
    [4] => 2
    [5] => 7
)

The two 86s and the two 67s are kept together, and within each tie the editions are ascending.

Sorting an array of rows by a column

Real datasets are often an array of associative rows. Use array_column() to pull out the columns you want to sort by, then pass the original array last so it is reordered to match:

$rows = [
    ['name' => 'Bob',   'age' => 30],
    ['name' => 'Alice', 'age' => 25],
    ['name' => 'Carol', 'age' => 30],
];

$age  = array_column($rows, 'age');
$name = array_column($rows, 'name');

// Primary: age ASC. Tie-break: name ASC.
array_multisort($age, SORT_ASC, $name, SORT_ASC, $rows);

print_r($rows);

Output:

Array
(
    [0] => Array ( [name] => Alice [age] => 25 )
    [1] => Array ( [name] => Bob   [age] => 30 )
    [2] => Array ( [name] => Carol [age] => 30 )
)

Gotcha: keys are reindexed

array_multisort() reindexes numeric keys (they become 0, 1, 2, …), but string keys are preserved. If you need to keep original integer keys, sort a copy of the keys alongside the data, or use a key-aware function like asort() instead.

When to use which sort function

  • Use array_multisort() when you have parallel arrays or need a multi-column sort.
  • Use sort() / rsort() for a single array by value when keys don't matter.
  • Use ksort() / asort() to sort by key, or by value while preserving keys.
  • Use usort() when one array needs custom comparison logic (e.g. sort by string length, or by a derived value) that flags can't express.

See the Sorting Arrays overview for a side-by-side comparison of all the sort functions.

graph LR
A[Array] --> B[Array Multisort]
B --> C[Ascending/Descending Order]
B --> D[Sorting by Key]
B --> E[Sorting by Multiple Criteria]

Practice

Practice
What does the array_multisort() function in PHP do?
What does the array_multisort() function in PHP do?
Was this page helpful?