1
    $timein_out = $this->time_model->get_timein_out($this->input->get('i'));   

    $total_diff= array();
    $hours = array();
    $mins =array();

    foreach($timein_out as $timetest)
     {
        $total_diff[] = strtotime($timetest["Time_out"]) - strtotime($timetest["Time_in"]);

        for($key=0;$key<count($total_diff);$key++)
        {
            $hours[] = intval(floor($total_diff[$key]/3600));
            $mins[] =  intval(($total_diff[$key]-$hours[$key]*3600)/60); 
        }

     };

     echo json_encode($total_diff); // output: [33600,34560,35160]
     echo json_encode($hours); // [9,9,9,9,9,9]
     echo json_encode($mins); //[20,20,36,20,36,46]

实际输出应该是

 echo json_encode($hours); // [9,9,9,]
 echo json_encode($mins); //[20,36,46]

问题:为什么输出重复?我的代码有什么问题?:( 谢谢。

4

2 回答 2

3

可能是你想要外面的内循环。尝试这个。

 foreach($timein_out as $timetest)
 {
    $total_diff[] = strtotime($timetest["Time_out"]) - strtotime($timetest["Time_in"]);

 }
 for($key=0;$key<count($total_diff);$key++)
 {
     $hours[] = intval(floor($total_diff[$key]/3600));
     $mins[] =  intval(($total_diff[$key]-$hours[$key]*3600)/60); 
 }
于 2013-09-23T03:23:19.490 回答
1

可以尝试unset()$total_diff类的:

foreach($timein_out as $timetest) {
    $total_diff[] = strtotime($timetest["Time_out"]) - strtotime($timetest["Time_in"]);

    for($key=0;$key<count($total_diff);$key++) {
        $hours[] = intval(floor($total_diff[$key]/3600));
        $mins[] =  intval(($total_diff[$key]-$hours[$key]*3600)/60); 
    }
    unset($total_diff);
}
于 2013-09-23T03:20:45.760 回答