Sorting an Array by Column Value in PHP: Everything You Need to Know

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Sorting an Array by Column Value in PHP: A Guide for Those Tired of Googling

\n\n

Have you ever encountered a situation where you have a list of users, products, or orders in the form of an array of associative arrays, and you need to sort them by some field? Say, by age, price, or registration date. If so — you are not alone. This question on Stack Overflow has gathered thousands of views and dozens of votes. And today we will break it down once and for all.

\n\n

In this article, we will not just show how usort and array_multisort work. We will answer the most common questions that developers have: how to sort by a nested key, how to preserve keys, how to sort by multiple fields, and what to do if the data comes from a database.

\n\n

Let's go!

\n\n

What is an Array of Associative Arrays and Why Sort It?

\n\n

Imagine you have a list of users:

\n\n
$users = [\n    ['name' => 'Anna', 'age' => 25, 'city' => 'Moscow'],\n    ['name' => 'Boris', 'age' => 30, 'city' => 'Saint Petersburg'],\n    ['name' => 'Victor', 'age' => 22, 'city' => 'Kazan'],\n];
\n\n

This is a typical multidimensional array in PHP, where each element is an associative array with the same keys. Often you need to display such a list sorted by some field: by age (from youngest to oldest), by name (in alphabetical order), or by city.

\n\n

Standard PHP functions like sort() or asort() won't help here — they work with simple values, not nested structures. Special tools are needed.

\n\n

The Most Popular Method: usort with an Anonymous Function

\n\n

If you go to Stack Overflow and open the discussion "Sort an array of associative arrays by column value", in 90% of cases you will be advised to use usort. This makes sense: the usort function allows you to define your own comparison logic for two array elements.

\n\n

How to Sort an Array by a Single Field (Age)

\n\n

Let's say we want to sort our users by age in ascending order:

\n\n
<?php\n$users = [\n    ['name' => 'Anna', 'age' => 25, 'city' => 'Moscow'],\n    ['name' => 'Boris', 'age' => 30, 'city' => 'Saint Petersburg'],\n    ['name' => 'Victor', 'age' => 22, 'city' => 'Kazan'],\n];\n\nusort($users, function($a, $b) {\n    return $a['age'] <=> $b['age'];\n});\n\nprint_r($users);\n?>
\n\n

Result:

\n
Array\n(\n    [0] => Array ( [name] => Victor [age] => 22 [city] => Kazan )\n    [1] => Array ( [name] => Anna [age] => 25 [city] => Moscow )\n    [2] => Array ( [name] => Boris [age] => 30 [city] => Saint Petersburg )\n)
\n\n

Note: we are using the spaceship operator <=>, which was introduced in PHP 7. It returns -1, 0, or 1, which is perfect for sorting functions. If you need to sort in descending order, simply swap $a and $b:

\n\n
usort($users, function($a, $b) {\n    return $b['age'] <=> $a['age'];\n});
\n\n

Sorting by a String Field (Name)

\n\n

For strings, it's just as simple. Want to sort users by name in alphabetical order? Here you go:

\n\n
usort($users, function($a, $b) {\n    return strcmp($a['name'], $b['name']);\n});
\n\n

Or use <=> again — it works with strings too:

\n\n
usort($users, function($a, $b) {\n    return $a['name'] <=> $b['name'];\n});
\n\n

Frequent Questions and Pitfalls

\n\n

1. How to Sort by a Nested Key?

\n\n

Sometimes the data has a deeper structure. For example, each user has a profile array

Blogs

Book Recommendations