PHP汇总JSON对象值
我知道这听起来很傻,但是我如何得到所有对象的总和呢 我的json解码格式如下所示:PHP汇总JSON对象值,php,json,Php,Json,我知道这听起来很傻,但是我如何得到所有对象的总和呢 我的json解码格式如下所示: Array ( [results] => Array ( [0] => Array ( [total] => 22 ) [1] => Array ( [total] => 10
Array
(
[results] => Array
(
[0] => Array
(
[total] => 22
)
[1] => Array
(
[total] => 10
)
)
)
我尝试过这样写东西,但它显示了“试图在…中获取非对象的属性”
echo“数组总数”;
echo 'Array Total<pre>';
$sum = 0;
foreach ( $receipt_data['results'] as $receipt )
{
$sum += $receipt['total'];
}
echo '</pre>';
$sum=0;
foreach($receipt_data['results']作为$receipt)
{
$sum+=$received->total;
}
回声';
试试这个,它适合你
您有数组,因此需要使用数组而不是对象
echo“数组总数”;
echo 'Array Total<pre>';
$sum = 0;
foreach ( $receipt_data['results'] as $receipt )
{
$sum += $receipt['total'];
}
echo '</pre>';
$sum=0;
foreach($receipt_data['results']作为$receipt)
{
$sum+=$received['total'];
}
回声';
sorry删除[0]并检查如果直接使用$receipt\u数据而不是$receipt\u数据['results'],则需要使用[0]