PHP array_multisort Function with Examples

php array_multisort

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order or text order. This helps you control how your arrays look after sort.

Syntax of PHP array_multisort

The PHP array_multisort function sorts one array or more arrays at the same time. It sets order for each array and returns a boolean value.

Here is an example:

array_multisort(array1, sort_order, sort_type, array2, ...);

You can sort a main array and also sort other arrays with it. This keeps related data in the right place after sort.

For example:

$numbers = [3, 1, 2];
$names = ["C", "A", "B"];
array_multisort($numbers, $names);
print_r($numbers);
print_r($names);

This code sorts $numbers in order and sorts $names in the same order. Both arrays stay linked after sort.

Here is the output:

Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => A
[1] => B
[2] => C
)

PHP array_multisort can sort numeric arrays by value or by flags like SORT_DESC. It changes the order of each array but keeps index links correct.

$values = [5, 2, 8, 1];
array_multisort($values, SORT_ASC);
print_r($values);

The code sets $values to [1,2,5,8] after sort. You can also use SORT_DESC for reverse order.

The output:

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

Using PHP array_multisort with associative arrays

You can sort an associative array by its values but keep keys safe. You can also match it with another array to keep data linked.

$age = ["Ali"=>30,"Sara"=>22,"Omar"=>25];
$name = ["Ali","Sara","Omar"];
array_multisort($age, SORT_ASC, $name);
print_r($age);
print_r($name);

This code sorts $age by value from low to high. The $name array stays matched with each key after sort.

The output:

Array
(
[Sara] => 22
[Omar] => 25
[Ali] => 30
)
Array
(
[0] => Sara
[1] => Omar
[2] => Ali
)

Sorting multidimensional arrays with PHP array_multisort

You can sort a multidimensional array by a column inside it. This helps order records like a table.

$data = [
  ["id"=>3,"name"=>"Ali"],
  ["id"=>1,"name"=>"Sara"],
  ["id"=>2,"name"=>"Omar"]
];
$ids = array_column($data, "id");
array_multisort($ids, SORT_ASC, $data);
print_r($data);

This code extracts the id column and sorts the main $data array by it. Each row keeps its own fields after sort.

The output:

Array
(
[0] => Array
(
[id] => 1
[name] => Sara
)

[1] => Array
(
[id] => 2
[name] => Omar
)

[2] => Array
(
[id] => 3
[name] => Ali
)

)

Examples

Sort two related arrays by numbers:

$scores = [45, 78, 12];
$names = ["Ali","Sara","Omar"];
array_multisort($scores, SORT_DESC, $names);
print_r($scores);
print_r($names);

This example sorts $scores from high to low. It also sorts $names in the same order to keep the link.

The output:

Array
(
[0] => 78
[1] => 45
[2] => 12
)
Array
(
[0] => Sara
[1] => Ali
[2] => Omar
)

Sort the associative array by age:

$people = ["Ali"=>30,"Sara"=>22,"Omar"=>25];
$names = array_keys($people);
$ages = array_values($people);
array_multisort($ages, SORT_ASC, $names);
print_r($names);
print_r($ages);

This example splits keys and values into arrays, sorts ages, and keeps names linked with each age.

The output:

Array
(
[0] => Sara
[1] => Omar
[2] => Ali
)
Array
(
[0] => 22
[1] => 25
[2] => 30
)

Sort a table by column:

$records = [
  ["id"=>2,"name"=>"Omar","score"=>80],
  ["id"=>1,"name"=>"Sara","score"=>90],
  ["id"=>3,"name"=>"Ali","score"=>70]
];
$scores = array_column($records,"score");
array_multisort($scores,SORT_DESC,$records);
print_r($records);

This example sorts $records by the score column from high to low. Each row stays matched with its values.

The output:

Array
(
[0] => Array
(
[id] => 1
[name] => Sara
[score] => 90
)

[1] => Array
(
[id] => 2
[name] => Omar
[score] => 80
)

[2] => Array
(
[id] => 3
[name] => Ali
[score] => 70
)

)

Sort many arrays with mixed order:

$first = [3,1,2];
$second = ["C","A","B"];
array_multisort($first,SORT_ASC,$second,SORT_DESC);
print_r($first);
print_r($second);

This example shows how you can set a different order for each array at once. It keeps data in place but changes the order.

The output:

Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => A
[1] => B
[2] => C
)

Wrapping Up

You learned what PHP array_multisort does and how to use it with many array types.

Here is a quick recap:

  • You can sort many arrays at once, keep links between arrays, and order multidimensional arrays by one column or more. This gives you full control of data order in your code.

FAQs

What is PHP array_multisort with example?

The array_multisort function sorts multiple arrays or multi-dimensional arrays. Here is an example:

<?php
$names = array("Sara", "John", "Alex");
$ages  = array(25, 30, 20);

array_multisort($ages, $names);

print_r($ages);
print_r($names);
?>
Output:
  • First array is sorted by values.
  • Second array is reordered based on first array sorting.

How does PHP array_multisort work?

The array_multisort function sorts arrays in parallel. It takes one array as the main sorting key and reorders the others.

<?php
$marks = array(90, 60, 70);
$students = array("Ali", "Mona", "Zaid");

array_multisort($marks, SORT_DESC, $students);

print_r($marks);
print_r($students);
?>
This sorts $marks in descending order and reorders $students accordingly.

Can PHP array_multisort sort multidimensional arrays?

Yes, it can sort multidimensional arrays. You pass sub-arrays as arguments and it sorts them by defined order.

<?php
$data = array(
  array("Ali", 22),
  array("Sara", 18),
  array("Omar", 25)
);

$names = array_column($data, 0);
$ages  = array_column($data, 1);

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

print_r($data);
?>
  • Sorts by age first.
  • Sorts by name next if ages match.

What are common flags in PHP array_multisort?

The function uses sorting flags to define order. Some common flags are:
  • SORT_ASC → Sort ascending order.
  • SORT_DESC → Sort descending order.
  • SORT_REGULAR → Compare items normally.
  • SORT_STRING → Compare items as strings.

<?php
$values = array("20", "9", "100");
array_multisort($values, SORT_ASC, SORT_STRING);
print_r($values);
?>
This forces string comparison instead of numeric.

Similar Reads

PHP Singleton Pattern: How to Use with Examples

The PHP singleton design pattern makes sure that the class has only one instance and provides a global access point…

PHP array_find_key: How it Works with Examples

The PHP array_find_key function finds a key in an array by a condition. It checks each element and returns the…

PHP str_repeat: How it Works with Examples

PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…

PHP array_diff_key Function: How it Works with Examples

PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values…

Polymorphism in PHP: How It Works with Examples

PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…

PHP array_diff_ukey Function: How it Works with Examples

The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…

PHP Spaceship Operator

If you are new to the PHP Spaceship Operator, then let me tell you, you're in for a treat. This…

Get Last Insert ID in PHP with MySQL PDO and MySQLi

Keeping track of the last record inserted is often important. Whether you are managing user registrations, processing orders, or handling…

PHP Comparison Operators Guide with Examples

PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…

PHP OOP Constructor: How It Works in a Class with Examples

The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

Previous Article

Git commit Command: How it Works with Examples

Next Article

SQL Server Collation: Types and Configuration

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.