# How to Sort a Multidimensional Array by Value?

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

# Sort Multidimensional Array by Value

Here’s the code from the article using the usort function to sort multidimensional array by value.

``````usort(\$employeesData, function(\$a, \$b)
{
return \$a["Salary"] - \$b["Salary"];
});
//OUTPUT
/*
[
["Name" => "Mosh", "Salary" => 120000],
["Name" => "Noah", "Salary" => 220000],
["Name" => "Dany", "Salary" => 320000],
["Name" => "Robert", "Salary" => 330000],
["Name" => "Jenny", "Salary" => 440000],
["Name" => "Kylie", "Salary" => 500000],
]
*/
``````

## An Example of a Multidimensional Array

A multidimensional array could be fairly complex, spanning many dimensions or arrays within arrays. However, let’s keep it realistic and simulate data that closely matches a database resultset.

A Database query often returns a multidimensional array of data. For instance, here’s a snippet of a database table having rows for employee names and their annual salaries.

Here’s how PHP gets this data as a multidimensional array.

``````<?php

\$employeesData =
[
["Name" => "Robert", "Salary" => 330000],
["Name" => "Kylie", "Salary" => 50000],
["Name" => "Mosh", "Salary" => 120000],
["Name" => "Jenny", "Salary" => 440000],
["Name" => "Dany", "Salary" => 320000],
["Name" => "Noah", "Salary" => 220000]
];

?>
``````

This array would serve as a good example for this article. So, let’s try to sort this multidimensional array in PHP.

## Sort Multidimensional Array by Value using usort

PHP usort function uses a user-defined function for sorting an array. It gives you more flexibility in defining logic for sorting an array.

`usort(array &\$array, callable \$callback): bool`

The function receives the array to be sorted and a callback function as.

`callback(\$a, \$b)`

The callback returns an integer depending on the comparison of \$a and \$b.

• Less than zero if \$a is less than \$b.
• Equals to zero if \$a is equal to \$b.
• Greater than zero if \$a is greater than \$b.

### Sort Multidimensional Array PHP by Column

Let’s use usort to sort multidimensional array PHP by column, “Salary”

``````usort(\$employeesData, function(\$a, \$b)
{
return \$a["Salary"] - \$b["Salary"];
});
//OUTPUT
/*
[
["Name" => "Mosh", "Salary" => 120000],
["Name" => "Noah", "Salary" => 220000],
["Name" => "Dany", "Salary" => 320000],
["Name" => "Robert", "Salary" => 330000],
["Name" => "Jenny", "Salary" => 440000],
["Name" => "Kylie", "Salary" => 500000],
]
*/
``````

The callback is simple, the subtraction yields a number greater than, equal to, or less than zero. The sorting happens on that basis, as given in the details of the callback function above. The sorting order is ascending. For descending order, just swap the callback arguments.

``````usort(\$employeesData, function(\$a, \$b)
{
return \$b["Salary"] - \$a["Salary"];
});

print_r(\$employeesData);

//OUTPUT
/*
[
["Name" => "Kylie", "Salary" => 500000],
["Name" => "Jenny", "Salary" => 440000],
["Name" => "Robert", "Salary" => 330000],
["Name" => "Dany", "Salary" => 320000],
["Name" => "Noah", "Salary" => 220000],
["Name" => "Mosh", "Salary" => 120000],
]
*/
``````

Users using PHP version >= 7 can also use the spaceship operator in the callback function as follows.

``````usort(\$employeesData, function(\$a, \$b)
{
return \$a["Salary"] <=> \$b["Salary"]; //Ascending order
});
``````

## Sort Multidimensional Array by Value using array_multisort

PHP array_multisort is yet another function for sorting multiple or multidimensional arrays. It can sort multidimensional array PHP by column but could be trickier to understand in one go.

The function takes an array followed by a couple of optional flags and then more arrays optionally followed by the same flags. The official document gives an overview of the flags. Let’s see how this function works before we dig into the code.

The function sorts all the arrays based on the first array. If there are identical elements, the sorting then relies on the second array and so on. It is a recurring algorithm. The function can sort a multidimensional array in PHP based on this algorithm.

### Sort Multidimensional Array PHP by Column

Let’s sort the array by the “Salary” column as we did in the last example.

``````array_multisort(array_column(\$employeesData, "Salary"), \$employeesData);
//OUTPUT
/*
[
["Name" => "Mosh", "Salary" => 120000],
["Name" => "Noah", "Salary" => 220000],
["Name" => "Dany", "Salary" => 320000],
["Name" => "Robert", "Salary" => 330000],
["Name" => "Jenny", "Salary" => 440000],
["Name" => "Kylie", "Salary" => 500000],
]
*/
``````

Voila! It works just fine. The function takes the “Salary” column as an array followed by the employeesData array. The function sorts the latter based on the former. The example uses the array_column function for isolating the “Salary” column out of the array.

Phew! We’re all good for now. You can pick any one of these two methods and sort a multidimensional array in PHP. So, it’s time for the wrap-up.

## Wrap Up

This article demonstrates how to sort a multidimensional array by value. First, it shows the usort function with an example followed by the array_multisort function. Both these methods can sort an array by a column of values.

Hopefully, you’ve enjoyed the article. Stay tuned for more articles like these at FuelingPHP.