Sorting an Array of Objects by a Single Property in PHP: A Complete Guide

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 of Objects in PHP: From Beginner to Pro

\n\n

Imagine a scenario: you are writing an online store in PHP. You have an array of products, each product is an object with fields price, name, rating. The user clicks "Sort by price," and you need to instantly rearrange this array. How to do this correctly and elegantly? That is exactly what today's guide is about.

\n\n

The topic of sorting an array of objects by a single property in PHP is one of the most popular queries on Stack Overflow (millions of views!). And it's no surprise: working with objects is the foundation of modern PHP, and the ability to flexibly sort data is a must-have skill. Let's break it all down from simple to complex, with live examples and explanations.

\n\n

Why can't you just use sort()?

\n\n

Beginners often try to apply sort() or rsort() to an array of objects. And they get an error or complete nonsense. Why? Because these functions compare elements as simple values (numbers or strings), while an object is a complex structure. PHP doesn't know which field you want to sort by: name, price, or date. You need to explicitly specify the comparison rule.

\n\n

That's exactly what the functions usort(), uasort(), and uksort() are for. The letter "u" in the name stands for "user-defined" — a user-defined comparison function. We write ourselves how to compare two objects.

\n\n

Basic example: sorting users by age

\n\n

Let's say we have a User class and an array of users:

\n\n
class User {\n    public $name;\n    public $age;\n    \n    public function __construct($name, $age) {\n        $this->name = $name;\n        $this->age = $age;\n    }\n}\n\n$users = [\n    new User('Anna', 25),\n    new User('Ivan', 30),\n    new User('Olga', 22),\n    new User('Peter', 28)\n];
\n\n

We need to sort them by age in ascending order. Let's use usort:

\n\n
usort($users, function($a, $b) {\n    return $a->age <=> $b->age;\n});\n\n// Result:\n// Olga (22), Anna (25), Peter (28), Ivan (30)
\n\n

Notice the spaceship operator ( <=> ) — it appeared in PHP 7.0 and makes the code incredibly concise. It returns -1, 0, or 1, which is perfect for comparison functions.

\n\n

Understanding the mechanism of usort

\n\n

The usort() function takes two arguments:

\n
    \n
  • Array (passed by reference — the original array will be modified)
  • \n
  • Callback function that compares two elements
  • \n
\n\n

The callback must return:

\n
    \n
  • A negative number (usually -1) — if the first element is less than the second
  • \n
  • 0 — if the elements are equal
  • \n
  • A positive number (usually 1) — if the first element is greater than the second
  • \n
\n\n

The spaceship operator does this automatically. Here is what the same sorting looks like without it:

\n\n
usort($users, function($a, $b) {\n    if ($a->age == $b->age) return 0;\n    return ($a->age < $b->age) ? -1 : 1;\n});
\n\n

The difference is obvious: the <=> operator reduces 4 lines to one. Use it everywhere where PHP 7+ is supported.

\n\n

Sorting by strings: considering case and locale

\n\n

With numbers, everything is simple, but strings require attention. Let's sort the same users by name:

\n\n
usort($users, function($a, $b) {\n    return strcmp($a->name, $b->name);\n});\n\n// Or with spaceship:\nusort($users, function($a, $b) {\n    return $a->name <=> $b->name;\n});
\n\n

But there is a nuance: spaceship is case-sensitive. The string "apple" will be less than "Banana" because the character code of 'a' (97) is less than 'B' (66). For case-insensitive sort

Blogs

Book Recommendations