PHP tutorials: array_map and array_filter

This can be a series of tutorials. I hope it will be. The credit for the initiative goes to the blog post “The Beginner Pattern“.

Since PHP 4.0.6, PHP has included two powerful functions to process arrays: array_map and array_filter. These can be a bit hard to wrap your head around at first, for they flip the normal thought pattern of “do foreach on the array, modifying each value” over, in the case of array_map, or “do foreach on the array, extract the elements that match something” in the case of array_filter.

array_map

This function takes at least two elements: a function and an array. After the first array, more arrays can be added. It returns an array with the elements resulting after applying the function to all the following arrays in parallel.

That was a mouthful. What that blurb meant to say is that a call to array_map("func", arr1, arr2, arr3), where func is a previously defined (or standard library) function that takes -in this case- 3 arrays, is  approximately equivalent to:

$len = count($arr1);
$result = array();
for ($i = 0; $i < $len, $i++) {
    $result[] = func($arr1[$i], $arr2[$i], $arr3[$i]);
}

I said “approximately” because the longest array passed dictates the length of the resulting array. You should make sure that all the arrays you pass to array_map as function arguments are of equal length, otherwise the shorter arrays will pass Null as the argument to func if they are exhausted. On the other hand, that might be exactly what you wanted.

One quick example:  Say you have a form to process. First thing you want to do is sanitize the input (let’s say it’s in $_POST)before processing it. So you build a sanitizer($unclean_input). Without array_map(), you have to do a loop as in the example above to apply your function to each element of $_POST. With array_map, you simply do

$_POST = array_map('sanitizer', $_POST);

and you’re done.

array_filter

Have you ever had to filter an array based on a condition? Did you ever look up to the skies and begged “Oh supreme lord of all creation, I’d like to be home by seven today, but this damned method is taking forever to code and I can’t see straight anymore!”?

Well, suddenly the holy light is shining upon your brow, and the lord is smiting your misbehaved code with array_filter.

This function takes an input array (the aptly named $input and a callback function. It iterates over each value of $input, passing it to $callback, and collecting every value for which $callback returns true in the result array. A quick example, lifted verbatim from PHP’s documentation:

< ?php
function odd($var) {
    return($var & 1);
}

function even($var) {
    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"));
?>

The output from this would be:

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)
Even:
Array
(
    [0] => 6
    [2] => 8
    [4] => 10
    [6] => 12
)

3 thoughts on “PHP tutorials: array_map and array_filter”

Comments are closed.