I have array like this
$a = array("15", "4", "3", "2");
to calculate I use array_sum($a);
how to use minus(-), devide(/) or multiple(*)
I want to 15 - 4 - 3 + 2 = 10
$result = 10;
Firstly, strings are wrapped in quotes, numeric values aren't. Secondly, just follow math rules.
$a = array(15, 4, 3, 2); // No quotes around numeric values
$b = $a[0] - $a[1] - $a[2] + $a[3]; // simple math
Using array_reduce() with a callback to implement the operator might be a logical approach, as long as the operator is consistent across all elements in the array (as it is with array_sum()).
For a -
operator, something like:
$a = array("15", "4", "3", "2");
$start = array_shift($a);
$result = array_reduce(
$a,
function($current, $value) {
return $current - $value;
},
$start
);