PHP 5.3 and later:
$arr = array(23,4,13,50,231,532,3); $arr_mod = array_map( function($val) { return $val * 0.4; }, $arr);
To dynamically transmit a dynamic coefficient, do:
$arr_mod = array_map( function($val, $factor) { return $val * $factor; }, $arr, array_fill(0, count($arr), 0.4) );
as the docs say:
The number of parameters that the callback function takes must match the number of arrays passed to array_map() .
In this simple example, this does not make much sense, but allows you to define a callback independently from each other, without any hard-coded values.
The callback will receive the corresponding values ββfrom each array that you pass into array_map() as arguments, so you can even use different values ββfor each value in $arr .
source share