From the course: Functional Programming with PHP

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Custom sorting behavior with usort

Custom sorting behavior with usort - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

Custom sorting behavior with usort

- [Instructor] So now we've seen how PHP's most basic built in sort function works. We pass it an array and it sorts it the best way it knows how and we can pass in flags if we want to tweak its behavior slightly. But sometimes we need more specific sorting functionality. As an example of this, what if we had an array of employee data? And wanted to sort our employees by how many years they've been at the company or by some other field? In order to allow us to customize our exact sorting behavior PHP provides us with another sort function that we can use called usort which allows us to define on a more specific level how the elements in our array should be sorted. We'll take a look at exactly how this works in a moment, keep in mind too that usort like sort is a mutating function. Which means that it actually modifies the original version of the array that it's called on. So how do we use usort? The syntax is pretty…

Contents