Sorting an Array by Column Value in PHP: A Guide for Those Tired of Googling
\n\nHave 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\nIn 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.
Let's go!
\n\nWhat is an Array of Associative Arrays and Why Sort It?
\n\nImagine 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\nThis 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\nStandard PHP functions like sort() or asort() won't help here — they work with simple values, not nested structures. Special tools are needed.
The Most Popular Method: usort with an Anonymous Function
\n\nIf 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.
How to Sort an Array by a Single Field (Age)
\n\nLet'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\nResult:
\nArray\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\nNote: 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:
usort($users, function($a, $b) {\n return $b['age'] <=> $a['age'];\n});\n\nSorting by a String Field (Name)
\n\nFor strings, it's just as simple. Want to sort users by name in alphabetical order? Here you go:
\n\nusort($users, function($a, $b) {\n return strcmp($a['name'], $b['name']);\n});\n\nOr use <=> again — it works with strings too:
usort($users, function($a, $b) {\n return $a['name'] <=> $b['name'];\n});\n\nFrequent Questions and Pitfalls
\n\n1. How to Sort by a Nested Key?
\n\nSometimes the data has a deeper structure. For example, each user has a profile array