I have print_r array like this. How do I SUM field ‘credit’ ?
I want to result like this in html :
2
you can create funtion to sum your array
function sum_credit($array){ $sum = 0; foreach($array as $val){ $sum += $val->credit; } return $sum; }
and print your data with
echo sum_credit($array);
$creditSum = array_sum(array_column($array,'credit'));
With array_column, a one-dimensional array is created from ‘credit’ values and then the sum is calculated with array_sum.
Click here to cancel reply.
2
Answers
you can create funtion to sum your array
and print your data with
With array_column, a one-dimensional array is created from ‘credit’ values and then the sum is calculated with array_sum.