如何在foreach循环中对项目求和?

时间:2016-06-28 00:12:57

标签: php foreach sum

foreach ($dives_file as $dives) {
    $type = explode(",", $dives);
    $divetype = $type[1];
    $duration = $type[2];
    $depth = $type[3];
}

如何获得所有$duration个数字的总和?

1 个答案:

答案 0 :(得分:0)

请将您的代码更新到下一个:

$duration = 0;

    foreach ($dives_file as $dives) 
    {
       $type = explode(",", $dives);
       $divetype = $type[1];
       $duration += $type[2];
       $depth = $type[3]; 
    }