PHP array_multisort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_multisort() sorts one array, or sorts several arrays together so that matching items stay in step.

It changes the arrays you pass in. When values in the first array tie, the next array is used to decide the order.

Syntax

Call array_multisort() with the values you need. Start with this line.

PHP
array_multisort($ages, $names);
CallWhat it does
array_multisort($numbers)Sorts $numbers from low to high.
array_multisort($numbers, SORT_DESC)Sorts $numbers from high to low.
array_multisort($ages, $names)Sorts by $ages first, then by $names for ties, keeping both in step.

The array is sorted in place and the function returns true. Always pass variables, not a freshly built array.

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

Parameters

ParameterDescription
array1Required. The first array to sort. It is passed by reference.
array1_sort_orderOptional. SORT_ASC (default) or SORT_DESC.
array1_sort_flagsOptional. A sort type such as SORT_REGULAR (default), SORT_NUMERIC or SORT_STRING.
restOptional. More arrays, each optionally followed by its own order and flags.

Return Value

Return ValueDescription
boolReturns true on success and false on failure.

PHP Examples

Example 1 — Sort One Array

Sort a list of numbers in place.

PHP
<?php
$numbers = [3, 1, 2];

array_multisort($numbers);

echo implode(", ", $numbers), PHP_EOL;

How It Works

  1. Create an unsorted array. It holds 3, 1 and 2.
  2. Call array_multisort(). It sorts $numbers directly.
  3. No assignment needed. The array is passed by reference, so the variable changes.
  4. Print the list. The numbers are now in ascending order.

Example 2 — Descending Order and String Keys

Use SORT_DESC for big-to-small, and notice what happens to string keys.

PHP
<?php
$a = [1, 3, 2];
array_multisort($a, SORT_DESC);
echo implode(", ", $a), PHP_EOL;

$b = ["x" => 3, "y" => 1, "z" => 2];
array_multisort($b);
echo json_encode($b), PHP_EOL;

How It Works

  1. Pass SORT_DESC. It sorts from the largest to the smallest.
  2. Print the first array. The result is 3, 2, 1.
  3. Sort an array with string keys. Values are sorted in ascending order.
  4. Check the keys. String keys stay attached to their values.

Example 3 — Sort Two Arrays Together

Sort people by age, then by name, keeping both arrays in step.

PHP
<?php
$ages  = [30, 25, 30];
$names = ["Cara", "Ana", "Bob"];

array_multisort($ages, $names);

for ($i = 0; $i < count($ages); $i++) {
    echo $names[$i] . " (" . $ages[$i] . ")" . PHP_EOL;
}

How It Works

  1. Create two matching arrays. Each position describes one person.
  2. Sort by ages first. 25 comes before 30.
  3. Break ties with the names. Both people aged 30 are ordered as Bob, then Cara.
  4. Loop with an index. Names and ages are still paired correctly.

Frequently Asked Questions

It sorts one array, or several arrays at once so that related items stay together. The arrays are changed in place.
It returns true on success and false on failure. The sorted result is in the arrays you passed.
Pass SORT_DESC after the array, like array_multisort($numbers, SORT_DESC).
All arrays given to array_multisort() must have the same number of elements.
Use usort() when each item is an array or object and you want to sort by a custom rule on a field.

Next: array_pad()

array_pad() makes an array a certain length by adding a value to its end or its start.

array_pad() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful