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 - PHP Tutorial
From the course: Learning PHP
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…
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
-
-
-
-
-
-
-
What are functions?5m 11s
-
(Locked)
Using built-in functions4m 40s
-
(Locked)
Writing custom functions7m 10s
-
(Locked)
Union typing variables2m
-
(Locked)
Optional or named arguments5m 23s
-
(Locked)
Anonymous functions4m 32s
-
(Locked)
What are objects and classes?3m 21s
-
(Locked)
Defining a simple person class (update for PHP 8.0)3m 19s
-
(Locked)
Using the person class3m 25s
-
(Locked)
Challenge: Sort an array of objects46s
-
(Locked)
Solution: Sort an array of objects3m 26s
-
-
-
-
-
-