Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abs of an array

Tags:

arrays

php

Which is the easy way of getting the abs of an array in php? It has to be a better way. This works, but in multidimensional array it has some limitations

function make_abs($numbers) {
 $abs_array = array();

 foreach($numbers as $key=>$value)
   $abs_array[$key] = abs($value);

 return $abs_array;
}
like image 584
dole doug Avatar asked Dec 03 '25 19:12

dole doug


1 Answers

Use a map function:

array_map("abs", $numbers)

http://php.net/manual/en/function.array-map.php

like image 160
ewok Avatar answered Dec 06 '25 09:12

ewok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!