array_map:

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

Description:

Php Code
array array_map ( callable $callback , array $array1 [, array $... ] )
[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:

Php Code
<?php
function cube($n)
{
return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>

This makes $b have:

Php Code
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)

array_filter

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

Description

Php Code
array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] )

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

Php Code
<?php

$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);

print_r(array_filter($entry));
?>
[ad type=”banner”]

The above example will output:

Php Code
Array
(
[0] => foo
[2] => -1
)

array_walk

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

Description :

Php Code
bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )
  • 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

Php Code
<?php

function odd($var) {
// returns whether the input integer is odd
return($var & 1);
}

function even($var) {
// returns whether the input integer is even
return(!($var & 1));
}

$array1 = array("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));

?>
[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: