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.
Sorting
- [Instructor] Now let's look at another important operation when working with arrays, sorting. Now to do sorting, we'll be using another one of PHP's built-in array functions, a function called, as you may have guessed, sort. This function is useful when we want to change the order of elements in an array. This would be very useful, for example, if we add in array of names and we wanted to sort it in alphabetical order, or if we simply wanted to sort an array of numbers. Before we look at how to use sort, it's very important to keep in mind that sort is a mutating function which means that the array that we pass to sort is passed by reference instead of by value, and actually gets modified. And that's just something to keep in mind since that interferes with the functional theme of immutability. So how do we use sort? Sort takes an array as an argument, as well as one or more sort flags that specify how the elements…
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
-
-
-
-