douji9734 2015-11-17 14:59
浏览 43

得到多维数组的总和

So i have an array which is created like this via a loop

foreach ($items as $item)
{
    $item_arr[$id]['count'] = $item->rowcount;
}

Now what i want to do is get the sum of the counts. I know i can just use $sum += $item->rowcount; but i was wondering if there was a more efficient way using something like this outside the loop when the foreach is done:

$sum = array_sum($item_arr[]['count']);

But that doesn't work says it doesn't like [], is there a way to do it or is the best way just to keep count in the foreach loop. Just would like to keep the code cleaner and more readable but maybe its a stupid question?

  • 写回答

3条回答 默认 最新

  • doure8758 2015-11-17 15:03
    关注

    Where did $id come from?

    the best would be $sum += $item->rowcount;

    $sum = 0;
    foreach ($items as $item) {
     $sum += $item->rowcount;
    }
    echo $sum;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示