From the course: Learning PHP

Unlock the full course today

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

Solution: Sort an array of objects

Solution: Sort an array of objects - PHP Tutorial

From the course: Learning PHP

Start my 1-month free trial

Solution: Sort an array of objects

(bright music) - [Instructor] All right, here's the solution to the challenge. You might have guessed that we're going to use the usort function and we're going to pass it the array people. So now we need to define an anonymous function. We'll use a and b again for the variables we're passing to it and we'll add our curly braces and our semi-colon to the end so we don't forget. Now the first solution that comes to mind is to use the spaceship operator that you saw previously. So we could say something like a get last name and then the spaceship operator b get last name and if we do this and then print out the results of the sort pre and then we'll add our php tags again and pre and then refresh the page. We'll see we do have a sorted list of objects by last name. Casabona, Casabona, Casabona, Disney, Gates, Iger and Wozniack, but the first three people are not in alphabetical order, they all have the…

Contents