我有一个对象数组,我想对其中一个属性的值求和,例如:

Array
(
[0] => stdClass Object
    (
        [name] => jon
        [commission] => 5

    )
[1] => stdClass Object
    (
        [name] => smith
        [commission] => 1

    )
[2] => stdClass Object
    (
        [name] => philip
        [commission] => 8

    )
)

我想将数组中的所有佣金相加,结果应为14。

有什么好办法吗?

最佳答案

$sum = 0;
foreach($arrObj as $key=>$value){
  if(isset($value->commission))
     $sum += $value->commission;
}
echo $sum;

关于php - 汇总数组中的对象属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20194259/

10-10 06:38