function cube($n)
{
return($n.="Hola");
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>
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 )
No hay comentarios:
Publicar un comentario