Home >Backend Development >PHP Tutorial >php-Arrays function-array_map-applies the callback function to the cells of the given array_PHP tutorial
array_map() applies the callback function to the given array unit
【Function】
This function will return an array,
This array contains all units in array1 after callback has been applied to them.
The number of parameters received by the callback function should be consistent with the number of arrays passed to the array_map() function.
【Scope of use】
php4>4.0.6, php5.
【Use】
array array_map( callback callback,array arr1[,array...] )
callback/required/a relatively standard callback function provided for users
arr1/required/array for comparison
array.../optional/array for comparison
【Example】
[php]
//Define callback function
function cube($n){
return ($n*$n*$n);
}
$a=array(1,2,3,4,5);
$b=array_map("cube",$a);
var_dump($b);
/*
array(5) {
[0]=>
int(1)
[1]=>
int(8)
[2]=>
int(27)
[3]=>
int(64)
[4]=>
int(125)
}
*/
Excerpted from zuodefeng’s notes