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.
Reducing - PHP Tutorial
From the course: Functional Programming with PHP
Reducing
- [Instructor] The final important array operation that we're going to learn about is something called reducing and to do it we're going to use yet another of PHP's built-in functions called array reduce. Now what reducing is might not be quite as apparent as with mapping, filtering, or sorting. But reducing is definitely just as useful as the other array functions. What reducing allows us to do is take an array of data and well reduce it down to a single piece of data. So we use reduce if we want to take an array of numbers and reduce it down to a sum, or an average for example. What array reduce PHP's built-in function for reducing does, is it starts with an initial value, say zero and for each element in the array it modifies this initial value in some way until after we've processed all our elements we end up with the result. In the case of finding the sum of an array of numbers, for example, we'd start with zero and add…
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
-
The spread operator and array_merge7m 37s
-
Mapping4m 46s
-
Filtering4m 58s
-
Passing keys to array functions4m 21s
-
Sorting3m 39s
-
Custom sorting behavior with usort6m 27s
-
Reducing6m 10s
-
Combining array functions6m 50s
-
Challenge: Recreating the array_map function1m 6s
-
Solution: Recreating the array_map function3m 40s
-
-
-
-