array_map:

(PHP 4 >= 4.0.6, PHP 5, PHP 7)
array_map — Applies the callback to the elements of the given arrays

Description:

[pastacode lang=”php” manual=”array%20array_map%20(%20callable%20%24callback%20%2C%20array%20%24array1%20%5B%2C%20array%20%24…%20%5D%20)%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

array_map() returns an array containing all the elements of array1 after applying the callback function to each one.

The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

Example #1

array_map() example:

[pastacode lang=”php” manual=”%3C%3Fphp%0Afunction%20cube(%24n)%0A%7B%0A%20%20%20%20return(%24n%20*%20%24n%20*%20%24n)%3B%0A%7D%0A%0A%24a%20%3D%20array(1%2C%202%2C%203%2C%204%2C%205)%3B%0A%24b%20%3D%20array_map(%22cube%22%2C%20%24a)%3B%0Aprint_r(%24b)%3B%0A%3F%3E%0A” message=”Php Code” highlight=”” provider=”manual”/]

This makes $b have:

[pastacode lang=”php” manual=”Array%0A(%0A%20%20%20%20%5B0%5D%20%3D%3E%201%0A%20%20%20%20%5B1%5D%20%3D%3E%208%0A%20%20%20%20%5B2%5D%20%3D%3E%2027%0A%20%20%20%20%5B3%5D%20%3D%3E%2064%0A%20%20%20%20%5B4%5D%20%3D%3E%20125%0A)%0A” message=”Php Code” highlight=”” provider=”manual”/]

array_filter

(PHP 4 >= 4.0.6, PHP 5, PHP 7)
array_filter — Filters elements of an array using a callback function

Description

[pastacode lang=”php” manual=”array%20array_filter%20(%20array%20%24array%20%5B%2C%20callable%20%24callback%20%5B%2C%20int%20%24flag%20%3D%200%20%5D%5D%20)%0A” message=”Php Code” highlight=”” provider=”manual”/]

Iterates over each value in the array passing them to the callback function.

If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.

Example #2

array_filter() without callback

[pastacode lang=”php” manual=”%3C%3Fphp%0A%0A%24entry%20%3D%20array(%0A%20%20%20%20%20%20%20%20%20%20%20%20%200%20%3D%3E%20’foo’%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%201%20%3D%3E%20false%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%202%20%3D%3E%20-1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%203%20%3D%3E%20null%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%204%20%3D%3E%20”%0A%20%20%20%20%20%20%20%20%20%20)%3B%0A%0Aprint_r(array_filter(%24entry))%3B%0A%3F%3E%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

The above example will output:

[pastacode lang=”php” manual=”Array%0A(%0A%20%20%20%20%5B0%5D%20%3D%3E%20foo%0A%20%20%20%20%5B2%5D%20%3D%3E%20-1%0A)%0A” message=”Php Code” highlight=”” provider=”manual”/]

array_walk

(PHP 4, PHP 5, PHP 7)
array_walk — Apply a user supplied function to every member of an array

Description :

[pastacode lang=”php” manual=”bool%20array_walk%20(%20array%20%26%24array%20%2C%20callable%20%24callback%20%5B%2C%20mixed%20%24userdata%20%3D%20NULL%20%5D%20)%0A” message=”Php Code” highlight=”” provider=”manual”/]
  • Applies the user-defined callback function to each element of the array array.
  • array_walk() is not affected by the internal array pointer of array.
  • array_walk() will walk through the entire array regardless of pointer position.

Example #1 

array_walk() example

[pastacode lang=”php” manual=”%3C%3Fphp%0A%20%0Afunction%20odd(%24var)%20%7B%0A%20%20%20%20%2F%2F%20returns%20whether%20the%20input%20integer%20is%20odd%0A%20%20%20%20return(%24var%20%26%201)%3B%0A%7D%0A%20%0Afunction%20even(%24var)%20%7B%0A%20%20%20%20%2F%2F%20returns%20whether%20the%20input%20integer%20is%20even%0A%20%20%20%20return(!(%24var%20%26%201))%3B%0A%7D%0A%20%0A%24array1%20%3D%20array(%22a%22%20%3D%3E%201%2C%20%22b%22%20%3D%3E%202%2C%20%22c%22%20%3D%3E%203%2C%20%22d%22%20%3D%3E%204%2C%20%22e%22%20%3D%3E%205)%3B%0A%24array2%20%3D%20array(6%2C%207%2C%208%2C%209%2C%2010%2C%2011%2C%2012)%3B%0A%20%0Aecho%20%22Odd%20%3A%5Cn%22%3B%0Aprint_r(array_filter(%24array1%2C%20%22odd%22))%3B%0Aecho%20%22Even%3A%5Cn%22%3B%0Aprint_r(array_filter(%24array2%2C%20%22even%22))%3B%0A%20%0A%3F%3E%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

The idea of mapping an function to array of data comes from functional programming.

  • You shouldn’t think about array_map as a foreach loop that calls a function on each element of the array.
  • It should be thought of as applying the function to each element in the array independently.

In theory such things as function mapping can be done in parallel since the function being applied to the data should ONLY effect the data and NOT the global state.

  • This is because an array_map could choose any order in which to apply the function to the items in (even though in PHP it doesn’t).

array_walk on the other hand it the exact opposite approach to handling arrays of data.

  • Instead of handling each item separately, it uses a state (&$userdata) and can edit the item in place (much like a foreach loop).
  • Since each time an item has the $funcname applied to it, it could change the global state of the program and therefor requires a single correct way of processing the items.

Back in PHP land, array_map and array_walk are almost identical except array_walk gives you more control over the iteration of data and is normally used to “change” the data in-place vs returning a new “changed” array.

array_filter is really an application of array_walk (or array_reduce) and it more-or-less just provided for convenience.

Categorized in: