【问题标题】:Sum of array in codeginitercodeigniter中的数组总和
【发布时间】:2016-06-21 11:22:35
【问题描述】:

如何在 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
)

【问题讨论】:

  • 你需要total_duration所有数组的总和对吗??
  • 是的@Yogesh Shakya

标签: php arrays codeigniter


【解决方案1】:

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);

【讨论】:

  • 显示结果为 00:00:0000:00:0000:00:0000:00:00
【解决方案2】:

这里是持续时间的总和

$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

希望这个答案对你有帮助

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2018-06-05
    • 1970-01-01
    • 2022-01-11
    • 2015-12-16
    • 1970-01-01
    • 2016-02-14
    • 2020-10-18
    • 2020-10-13
    相关资源
    最近更新 更多