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.

Passing keys to array functions

Passing keys to array functions - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

Passing keys to array functions

- [Instructor] Now that we've seen the basic use cases for array map and array filter, one thing that we sometimes need to do when we're working with these functions is get access to the key or index of the current element that we're looking at. As we've seen previously, by default array map and array filter provide the actual elements themselves, the array values, to whatever function we pass. But sometimes we need to know the keys of the elements as well. And we're going to take a look at how to do this in PHP. The technique we use for passing the keys will differ between our map and filter functions. So we'll start out with array filter. It's actually quite easy to get access to the keys in array filter. We simply have to pass an extra flag parameter as the third argument. Now there are two different flags that we can pass here, array filter use key and array filter use both. What array filter use key does, is…

Contents