★ wanayoo — archive 1999 http://fr.php.net/manual/cs/function.array-map.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousarray_keysarray_merge_recursivenext
Last updated: Sun, 01 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

array_map

(PHP 4 >= 4.0.6)

array_map --  Applies the callback to the elements of the given arrays

Description

array array_map ( mixed callback, array arr1 [, array arr2...])

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

Příklad 1. array_map() example

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:
Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)

Příklad 2. array_map() - using more arrays

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

function map_Spanish($n, $m) {
    return array ($n => $m);
}

$a = array(1, 2, 3, 4, 5);
$b = array("uno", "dos", "tres", "cuatro", "cinco");

$c = array_map("show_Spanish", $a, $b);
print_r($c);

$d = array_map("map_Spanish", $a , $b);
print_r($d);

This results:
// printout of $c
Array
(
    [0] => The number 1 is called uno in Spanish
    [1] => The number 2 is called dos in Spanish
    [2] => The number 3 is called tres in Spanish
    [3] => The number 4 is called cuatro in Spanish
    [4] => The number 5 is called cinco in Spanish
)

// printout of $d
Array
(
    [0] => Array
        (
            [1] => uno
        )

    [1] => Array
        (
            [2] => dos
        )

    [2] => Array
        (
            [3] => tres
        )

    [3] => Array
        (
            [4] => cuatro
        )

    [4] => Array
        (
            [5] => cinco
        )

)

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

Příklad 3. Creating an array of arrays

$a = array(1, 2, 3, 4, 5);
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");

$d = array_map(null, $a, $b, $c);
print_r($d);

The printout of the program above will be:
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => one
            [2] => uno
        )

    [1] => Array
        (
            [0] => 2
            [1] => two
            [2] => dos
        )

    [2] => Array
        (
            [0] => 3
            [1] => three
            [2] => tres
        )

    [3] => Array
        (
            [0] => 4
            [1] => four
            [2] => cuatro
        )

    [4] => Array
        (
            [0] => 5
            [1] => five
            [2] => cinco
        )

)

See also array_filter() and array_reduce().

User Contributed Notes
array_map
add a note about notes
jkohen @t users d.t sf d.t net
08-Jan-2002 06:44

Be warned the callback function can't be one internal to PHP. If you want to pass an internal function as a callback to array_map (and probably others that take callback functions), you have to wrap it around with a custom function.

For example:
function mes_wrapper($str) {
return mysql_escape_string($str);
}
array_map('mes_wrapper', array(...));

bishop
26-Mar-2002 09:33

Sometimes you might be interested in performing a "deep" map, where you apply the callback to all elements in the array, even if the elements are stuck deep inside of some array(array(array(...))) business.

Here's an example PHP4 function that handles this case, with a little bit more flexible (and potentially more error-prone) callback parameter:

function deepmap($cb, $x) {

if (is_array($x)) {
$it = array();
foreach ($x as $k => $v) {
$it["$k"] = deepmap($cb, $x["$k"]);
}
return $it;
} else {
$cmd = "return (" .
str_replace('$__', $x, $cb) .
");";
return eval($cmd);
}

}

So, you could do, for example:

$some_odds = array(1, 3, 5);
$some_evens = array(0, 2, 4, 6);
$some_nums = array($some_odds, array($some_evens));

deepmap('pow($__, 2)', $some_odds);
deepmap('pow($__, 2)', $some_nums);

Both examples square each element of the arrays, making sure to keep the array structure intact.

Note that the callback function needs to be a) quoted, and b) include the special string $__ (dollar double underscore); deepmap replaces $__ in the function call with whatever the current element is.

shane at sell d.t com
22-May-2002 09:26

With the help of an associate of mine, I discovered that array_map, like array_filter, takes an array with an object reference and method as the callback.

ie:

array_map( array($this, "callback"), $arr );

dan at mojavelinux dot com
15-Jun-2002 04:07

Here is a better, more true version of a deep array_map. The only negative of this function is that the array is passed by reference, so just be aware of that. (patches welcome)

function array_map_deep(&$in_array, $in_func, $in_args = array(), $in_index = 1) {
// fix people from messing up the index of the value
if ($in_index < 1) {
$in_index = 1;
}

foreach (array_keys($in_array) as $key) {
// we need a reference, not a copy, normal foreach won't do
$value =& $in_array[$key];
// we need to copy args because we are doing
// manipulation on it farther down
$args = $in_args;
if (is_array($value)) {
array_map_deep($value, $in_func, $in_args, $in_index);
}
else {
array_splice($args, $in_index - 1, $in_index - 1, $value);
$value = call_user_func_array($in_func, $args);
}
}

return $in_array;
}

This is a neat function because you can pass an array, a function, and an array of parameters, and finally, and index of where in the array of parameters for the callback function the contents you are mapping should get replaced. This index is human based (starts at 1), and can be used in something like a preg_replace callback, where the contents must be the 3rd index. Enjoy!

add a note about notes
previousarray_keysarray_merge_recursivenext
Last updated: Sun, 01 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Sat Aug 31 06:19:44 2002 CEST