array_map

Description

array_map() returns an array containing all
the elements of arr1 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()

Parameters

callback

Callback function to run for each element in each array.

arr1

An array to run through the callback function.

array

Variable list of array arguments to run through the
callback function.

Return Values

Returns an array containing all the elements of arr1
after applying the callback function to each one.

Examples

Example #1 array_map() example

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

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

Example #2 array_map() using a lambda function (as of PHP 5.3.0)

<?php$func = function($value) { return $value * 2;};

print_r(array_map($func, range(1, 5)));?>

Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)

Example #3 array_map() - using more arrays

<?phpfunction show_Spanish($n, $m){ return("The number $n is called $m in Spanish");}

Usually when using two or more arrays, they should be of equal length
because the callback function is applied in parallel to the corresponding
elements.
If the arrays are of unequal length, the shortest one will be extended
with empty elements.

An interesting use of this function is to construct an array of arrays,
which can be easily performed by using NULL
as the name of the callback function

If the array argument contains string keys then the returned array
will contain string keys if and only if exactly one array is passed. If
more than one argument is passed then the returned array always has
integer keys.

This function behaves exactly like array_map but additionally does not reject non-array arguments. Instead, it transforms them with the array_fill function to a constant valued array of required length according to the other array arguments (if any) and executes the original array_map function.

I came up with a convenient syntax for method application, particularly useful inside the array_map.For instance, if you want toarray_map(function($object, $arg1) { return $object->method($arg1, "arg2"); }, $objects, $args1);

it is possible to provide a shorthand:global $_; // necessary inside a function, unfortunately

Hope I'm not late to the party, here's my function to apply array_map to the *keys* of an array.Extra array arguments will be used for the callback function's parameters just like with array_map, with the difference that a string is also allowed: it will just be used to create an array of appropriate length with as each value that string. Arrays are left alone (and will be padded with nulls by array_map as needed).

Wrote up my own key preservation function for array mapping. It allows n arguments to be passed, and should be easy enough to follow if you need to make any mods. If you've got any thoughts let me know.

The following takes an array of objects, and returns the result of calling a member function on each object. So if I have an array of objects that all have a getName() method, calling array_map_objects("getName", $thingies) will return the array filled with the getName() value for each object.

You can apply a method of a instantiated class to array_maps as follows:

class Maths { function addOne($input) { return ($input + 1); }}$maths = new Maths();$sum = array_map(array($maths, \\\'addOne\\\'), array(1, 2));// where $maths is the object which has been instantiated before and addOne is its method without its own parametersvar_dump($sum);

The code fragment will return:

array 0 => 2 1 => 3

However, I love a syntax like this:

$sum = array_map($maths->addOne($this), array(1, 2));

where $this should be interpreted as each values extracted from the subsequent array, which in this case is array(1, 2).

// Validating parametersforeach($args as $key => $arg) if(is_array($arg)) {// the first array found gives the size of mapping and the keys that will be used for the resulting arrayif(!isset($size)) {$keys = array_keys($arg) ;$size = count($arg) ;// the others arrays must have the same dimension} elseif(count($arg) != $size) { return FALSE ; }// all keys are suppressed$args[$key] = array_values($arg) ; }