从二维数组中找出时间总和

时间:2013-12-04 13:25:26

标签: php

您好我会找到总和,但显示错误如Invalid argument supplied for foreach()如何ckeck错误

我的代码是

我有时间这样

$SQL = "SELECT o_duration FROM total WHERE location=('".$location_c[$k]."') AND sub_cat=('".$id."') AND dateid BETWEEN '".$trgD."' AND '".$date2."'";
$result = mysql_query($SQL);

$s="0";
$duration_c[$k]=array();
while ($db_field = mysql_fetch_array($result)) {
$duration_c[$k][$s]= $db_field['o_duration'];

$s++;
}

比我用带有for循环的表格打印它

<?php
for($i=0;$i<$l;$i++)
{ 

?>
<td width="154" class="alt" align="center"><?php  echo sum_the_time($duration_c[$i])
?></td> <?php } ?>



function sum_the_time($times) {

  $seconds = 0;
  foreach ($times as $time1)
  { 
list($hour,$minute,$second) = explode(':', $time);
$seconds += $hour*3600;
$seconds += $minute*60;
$seconds += $second;
 }
 $hours = floor($seconds/3600);
 $seconds -= $hours*3600;
 $minutes  = floor($seconds/60);
 $seconds -= $minutes*60;
 // return "{$hours}:{$minutes}:{$seconds}";
 return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
 }

1 个答案:

答案 0 :(得分:0)

Invalid argument supplied for foreach()表示您传递给函数的数组为空(或不是数组)

添加条件以检查其是否有效

if(is_array($times) and count($times)>0){
  foreach ($times as $time1){
    (...)
  }
}