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 - PHP Tutorial
From the course: Functional Programming with PHP
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Working with arrays, functionally1m 42s
-
(Locked)
The spread operator and array_merge7m 37s
-
(Locked)
Mapping4m 46s
-
(Locked)
Filtering4m 58s
-
(Locked)
Passing keys to array functions4m 21s
-
(Locked)
Sorting3m 39s
-
(Locked)
Custom sorting behavior with usort6m 27s
-
(Locked)
Reducing6m 10s
-
(Locked)
Combining array functions6m 50s
-
(Locked)
Challenge: Recreating the array_map function1m 6s
-
(Locked)
Solution: Recreating the array_map function3m 40s
-
-
-
-