php : Array_map
Array_map send each value of an array to a function, multiply each value by itself, and return an array with the new values.
Simply, but not simple
Ok, one function one usage. Not really true, you could really use this function for another usage
- Call inside a class
- Use external function on another class
- Put some extra datas on parameters
Simple usage
function cube($n)
{
return($n * $n * $n);
}
$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
Call inside a class
External function
Syntax : array_map( array( __CLASS__, 'methodName' ), $array );
Example : array_map(array($this, 'dash'), $data);
Use external function on another class
Syntax : array_map( array( 'ClassName', 'methodName' ), $array );
Use extra parameters (datax : array)
function dash($data1,$data2)
{
…
}
array_map(array($this, 'dash'), $data1, $data2);
Could also seen on
array_walk(), call_user_func(), call_user_func_array()