codeginiter中的数组总和

时间:2016-06-21 11:22:35

标签: php arrays codeigniter

如何在CodeIgniter中获取所有total_duration的总和。

Array
(
    [caller] => Ravneet Kaur
    [total_calls] => 1
    [total_duration] => 00:00:06
)

Array
(
    [caller] => Navneet Kaur
    [total_calls] => 2
    [total_duration] => 00:00:19
)

Array
(
    [caller] => Jasmeet Kaur
    [total_calls] => 0
    [total_duration] => 00:00:00
)

Array
(
    [caller] => sunny Kumar
    [total_calls] => 0
    [total_duration] => 00:00:00
)

2 个答案:

答案 0 :(得分:0)

answer可能有助于在array_column

的帮助下解决您的问题

检查一下:

$total = array_column($array, 'total_duration'); 
$sum = strtotime('00:00:00');
$sum2=0;  
foreach ($total as $v){
    $sum1=strtotime($v)-$sum;
    $sum2 = $sum2+$sum1;
}

$sum3=$sum+$sum2;

echo date("H:i:s",$sum3);

答案 1 :(得分:0)

这是持续时间的总和

$seconds= 0;
foreach ($new_arr as $key => $value) { 
   list($hour,$minute,$second) = explode(':',$value["total_duration"]);                                               
   $seconds += $hour*3600;
   $seconds += $minute*60;
   $seconds += $second;

}           

$hours = floor($seconds/3600);
$seconds -= $hours*3600;
$minutes  = floor($seconds/60);
$seconds -= $minutes*60;

echo  sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);

你可以在这里查看答案=> https://eval.in/592792

希望这个答案可以帮到你