Introduction
Sorting arrays in PHP is straightforward when you’re dealing with simple arrays; however, it becomes a bit tricky when you have an array of objects. Objects bring complexity as you need to sort based on property values, which can be string, integer, or even other objects. In this tutorial, we will explore various ways to sort an array of objects by their property values in PHP.
Understanding the Basics
Before we dive into the sorting techniques, let’s understand what we mean by an array of objects in PHP. PHP, an object-oriented language, allows us to create classes and instantiate objects which we can collect into arrays.
class Product {
public $name;
public $price;
public function __construct($name, $price) {
$this->name = $name;
$this->price = $price;
}
}
$products = [
new Product('Desk', 200),
new Product('Chair', 100),
new Product('Lamp', 50)
];
This gives us an array, $products
, containing Product objects with different property values.
Using usort to Sort by Object Property
PHP provides a function called usort
which allows us to sort using a custom comparison function. This makes it perfect for sorting objects.
usort($products, function($a, $b) {
return $a->price > $b->price;
});
After running the above code, $products
would be sorted ascendingly by the price
property. If you want to sort descendingly, simply invert the comparison:
usort($products, function($a, $b) {
return $b->price > $a->price;
});
Sorting by Multiple Properties Using usort
If you need to sort by multiple properties, you can extend the comparison function:
usort($products, function ($a, $b) {
if ($a->price == $b->price) {
return $a->name > $b->name ? 1 : -1;
}
return $a->price > $b->price ? 1 : -1;
});
In this example, products are primarily sorted by price
. If two products have the same price, the name
property is used as a secondary sorting criterion.
Using Array Multisort
If extracting properties to an array suits your use case, array_multisort
can be efficient. Here’s how:
$prices = array_column($products, 'price');
array_multisort($prices, SORT_ASC, $products);
First, we extract the price
property values using array_column
into the $prices
array. Then array_multisort
is used to sort both the $prices
array and the $products
array in unison.
Object-oriented Approach
We can encapsulate sorting logic within the class itself, which can be helpful for objects that always need to be sorted in a specific way:
class Product {
// Properties and constructor omitted for brevity.
public static function sort_by_price(&$products) {
usort($products, function($a, $b) {
return $a->price > $b->price;
});
}
}
Product::sort_by_price($products);
This static method can now be re-used to sort $products
arrays throughout your application.
Sorting with the spaceship operator
From PHP 7 onwards, we can simplify our code by using the spaceship operator (<=>
), which is ideal for sorting:
usort($products, function($a, $b) {
return $a->price <=> $b->price;
});
This operator will return 0 if both sides are equal, a negative number if the left side is smaller, and a positive number if the left side is greater. It greatly simplifies comparison logic.
Conclusion
In this tutorial, we reviewed multiple methods to sort an array of objects by property values in PHP, including the use of usort
, array_multisort
, object-oriented approaches, and PHP 7’s spaceship operator. Each method has its own use cases and advantages. Understanding these sorting techniques can greatly improve the efficiency and readability of your PHP applications. Happy coding!