[英]PHP How to total up the sum from each balance in a foreach loop
code that needs to be totaled up $v['balance']
需要总计的代码$v['balance']
foreach ($ss['result']['stats'] as $k => $v) {
print array_sum($v['balance']);
}
the current code prints the following: 当前代码显示以下内容:
0
0.00000938
0.0000007
0.00013408
0.00002358
0.00012234
0.00001106
0.00000159
which is correct. 哪个是对的。 I now want to total up all of them. 我现在想总结所有这些。 I have tried multiple different ways and they either display the following: 我尝试了多种不同的方法,它们要么显示以下内容:
foreach ($ss['result']['stats'] as $k => $v) {
print $v['balance']+$v['balance']."<br/>";
}
print 打印
0
1.876E-5
1.42E-6
0.00026816
4.716E-5
0.00025224
2.724E-5
3.18E-6
am I doing something incorrectly ? 我做错了什么吗?
$total = 0;
foreach ($ss['result']['stats'] as $k => $v) {
print array_sum($v['balance']);
$total = $total + $v['balance'];
}
print $total;
Just use a variable from the outer scope of the iteration. 只需使用迭代外部范围中的变量即可。
$total = 0;
foreach ($ss['result']['stats'] as $k => $v) {
$total += array_sum($v['balance']);
print array_sum($v['balance']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.