I have the result from database.
Array
(
[0] = stdClass Object
(
[name] = First
[sum] = 3,8,...
)
[1] = stdClass Object
(
[name] = Second
[sum] = -1,0,...
)
[2] = stdClass Object
(
[name] = Third
[sum] = 2,-1...
)
)
So now I need to sum all in column "sum".
I need to get result like
$final = (4, 7,...);
I have transformed sum to array throw explode() and then tried with foreach
for example
foreach ($result as $k=>$subArray) {
$arrayNumbers = explode(",",$subArray->sum);
foreach ($arrayNumbers as $key => $value) {
$sumArray[] = $value];
$stepToSum2[] = array_sum($sumArray);
}
unset($arrayNumb);
}
Not sure that my example working because I'm already stuck with commented code.
Anyway, I with some manipulations I can get or sum right for the first numbers (5) or the sum of my array (11).
The same result with this
$sum = array_sum(array_map(function($var) {
return $var['sum'];
}, $myResultArray));
I have searched for the answer but most of the answers only for two arrays, but in same tables, I have more than 5 arrays, so I can't figure out how to implement this.