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.

Solution: Recreating the array_map function

Solution: Recreating the array_map function - PHP Tutorial

From the course: Functional Programming with PHP

Start my 1-month free trial

Solution: Recreating the array_map function

(Upbeat music) [Narrator]- Okay, so let's see the solution to the challenge of recreating the array map function in PHP. We'll start with the easier solution of using a for loop. inside our function body, the first thing we want to do is to find a new array that will hold the mapped values. We'll call this new array, and it will initially be an empty array and what we want to do then is iterate through all the elements in the array using the old index method from procedural programming. For this we'll use a for loop, we'll say for I equals zero, I is less than count array and I plus equals one and then for each element in our array we want to get the result of calling our function argument on the element and that will look like this. We'll say result equals function called on our array at index I and then we want to push this result on to our new array. So we'll say new array, brackets equals…

Contents