From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Custom sort an array with the spaceship operator

Custom sort an array with the spaceship operator

From the course: PHP Tips, Tricks, and Techniques

Custom sort an array with the spaceship operator

- [David Powers] Hi, I'm David Powers and welcome to this week's addition of PHP Tips, Tricks, and Techniques designed to help you become a smarter, more productive PHP developer. This week I want to look at how to do a customized sort of an array using the spaceship operator, which is new to PHP 7. I'll also show you how to do it in PHP 5. In the download files for this video, there's this multidimensional array called members. At each sub-array is an associative array with two elements. First name and last name. What I want to do is to sort this array so the members names are sorted alphabetically, as you can see at the moment, they're all jumbled up. Now in this example, the array is hard coded into the script. In the real world, arrays usually come from external resources. If it came from a data base, the obvious solution would be to craft an SQL query to extract the values in the correct order. But we don't always have control over the data we need to process. And that's why it's…

Contents