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

Reducing - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

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…

Contents