如何使用PHP对数组的对象属性求和 [英] How can I sum objects property of an array using PHP

查看:385
本文介绍了如何使用PHP对数组的对象属性求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,我想对一个属性的值求和.这里是一张图片,它将显示数组的结构.

I have an array of objects, and i want to sum value of one of the property.Here is a picture which will show the structre of array.

这是我的代码,不起作用.

print_r($res);//this appear the structure of array,which i will show.   
$sum = 0;   
foreach($res as $key=>$value){ 
   if(isset($value->sent))   
        $sum += $value->sent;
   }   
echo $sum;

推荐答案

$sum = 0;
$result=$res->intervalStats;
foreach($result as $key=>$value){

if(isset($value->spent))   
    $sum += $value->spent;
}
echo $sum;

这篇关于如何使用PHP对数组的对象属性求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆