Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

PHP usort() function (with example)

Hey everyone,

I hope you're all doing well. I have a question about the PHP usort() function and I was hoping someone could help me out.

I've been working on a web application where I need to sort an array of elements based on a specific comparison function. After some research, I came across the usort() function in PHP that seems to do exactly what I need.

However, I'm not quite sure how to utilize this function properly. I would really appreciate it if someone could provide me with an example of how to use the usort() function in PHP. It would be great if you could explain the syntax and parameters of the function as well.

Thank you in advance for your help!

All Replies


Hey there,

I see that you're looking for help with the PHP usort() function. I've actually used it in a project recently, so I thought I'd share my experience.

To use the usort() function, you need to pass two parameters: the array you want to sort and a callback function that defines the custom comparison logic. The callback function should return an integer less than, equal to, or greater than zero based on the comparison of two elements.

For example, let's say we have an array of student objects with a "score" property. We want to sort the array based on the descending order of scores. Here's how you can achieve that using the usort() function:

$students = [
['name' => 'John', 'score' => 85],
['name' => 'Jane', 'score' => 92],
['name' => 'Mike', 'score' => 78],
['name' => 'Sarah', 'score' => 88],

usort($students, function($a, $b) {
return $b['score'] - $a['score'];


In this example, the callback function compares the "score" property of two student objects and returns the difference. By returning `$b['score'] - $a['score']`, we ensure a descending order.

When you run this code, you'll see that the `$students` array will be sorted based on the "score" property in descending order.

I hope my example helps you understand how to use the usort() function. Feel free to ask if you have any further questions!


Hey there,

I stumbled upon this thread and noticed that you're seeking help with the PHP usort() function. I thought I'd share my personal experience with it, as I've encountered a unique use case for this function.

In one of my projects, I needed to sort an array of objects based on a particular property, but with a slight twist. The property in question was a nested array within each object, and I needed to sort based on a specific key within that nested array.

After researching, I found that the usort() function was perfect for this task. To accomplish it, I had to create a callback function that would access the desired nested key for comparison. Here's a simplified version of what I did:

$items = [
['name' => 'Apple', 'details' => ['color' => 'red', 'price' => 2.50]],
['name' => 'Banana', 'details' => ['color' => 'yellow', 'price' => 1.75]],
['name' => 'Orange', 'details' => ['color' => 'orange', 'price' => 2.00]],

usort($items, function($a, $b) {
$nestedKey = 'price'; // Specify the nested key you want to sort by
return $a['details'][$nestedKey] - $b['details'][$nestedKey];


In this case, I wanted to sort the `$items` array based on the "price" key within the "details" nested array. The usort() function uses the callback function to compare the nested "price" values and sort the objects accordingly.

When you run this code, you'll notice that the `$items` array will be sorted based on the "price" key within the "details" array, in ascending order.

I hope this example gives you a different perspective on how the usort() function can be applied to unique scenarios. Let me know if you have any questions or need further clarification!

New to LearnPHP.org Community?

Join the community