array_map

Description

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()

Parameters

callback

Callback function to run for each element in each array.

array1

An array to run through the callback function.

...

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

Return Values

Returns an array containing all the elements of array1
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, shorter ones will be extended with empty
elements to match the length of the longest.

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

You may be looking for a method to extract values of a multidimensional array on a conditional basis (i.e. a mixture between array_map and array_filter) other than a for/foreach loop. If so, you can take advantage of the fact that 1) the callback method on array_map returns null if no explicit return value is specified (as with everything else) and 2) array_filter with no arguments removes falsy values.

This will take the original array and perform an action only on items specified on the second array items. Use of & symbol in the use statement makes the array_map access the variable as a reference in an outer scope.

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.

Essentially the collection you send in can be sent to the same var you operate after map has completed, thus explicitly overwriting, but importantly the input array is not modified, so the method is (as it should be) immutable. This means if you send in `$array1` and assign result to `$array2`, you'll still have `$array1` as it was prior to the map function call.

This is entirely separate from foreach modification semantics where mostly I've seen a mix of immutable (assigning to a new object) and mutable (using key and array to update in-place `$arrayinput[$key] = doSomethingWith($value)`.

This is not a comment on the internal workings but has been tested on 5.4, 5.6, 7.0 and 7.1

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).

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) ; }

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.