PHP总结JSON对象值 [英] PHP Sum up JSON Object value
本文介绍了PHP总结JSON对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这听起来很愚蠢,但是如何获得所有对象总数的总和?
I know this sounds silly but how do I get the total sum of all the object's total?
我的json解码格式如下:
My json decode format looks something like this:
Array
(
[results] => Array
(
[0] => Array
(
[total] => 22
)
[1] => Array
(
[total] => 10
)
)
)
我尝试使用类似这样的方式写,但是它显示试图在...中获取非对象的属性"
I've tried using writing something like this but it shows "Trying to get property of non-object in..."
echo 'Array Total<pre>';
$sum = 0;
foreach ( $receipt_data['results'] as $receipt )
{
$sum += $receipt->total;
}
echo '</pre>';
推荐答案
试试看,它对您有用 您有数组,因此需要使用数组而不是对象.
Try this it's works for you You have array so you need to use array instead of object.
echo 'Array Total<pre>';
$sum = 0;
foreach ( $receipt_data['results'] as $receipt )
{
$sum += $receipt['total'];
}
echo '</pre>';
这篇关于PHP总结JSON对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文