PHP求和函数?

时间:2014-03-15 18:56:29

标签: php function sum

我想要对我的数组的值求和? 如果有任何php函数,则对现有多个(多个值)

的变量值求和

看看这是我的完整例子。我从mysql中获取check_in和check_out值然后我计算小时,分钟和秒的差异......之后我怎样才能添加小时,分钟和秒的所有值....?像这样

我需要这样的输出     小时= 3     分钟= 36     秒= 55

for($i=0;$i<count($display_time); $i++)
        {
        $timeStart = $display_time[$i]["check_in"];
        $timeEnd =  $display_time[$i]["check_out"];
        ?>
        <tr>
        <td><?php echo $value ++; ?></td>
        <td><?php echo $display_time[$i]["date"];?></td>  
        <td><?php echo $display_time[$i]["check_in"]; ?></td>  
        <td><?php echo $display_time[$i]["check_out"]; ?></td>
        <td><?php



         $to_time = strtotime($timeStart);
        $from_time = strtotime($timeEnd);
        echo $time_in_hour = round(abs($from_time - $to_time) / (60 * 60)). " hour ";



        echo $time_in_minute = round(abs(($from_time - $to_time) / 60)%60). " minute ";
        echo $time_in_second = round(abs(($from_time - $to_time) / 60*60)%60). " secs ";




         $total_time = array($time_in_hour, $time_in_minute, $time_in_second);


            print_r($total_time);
                Array
                (
                    [0] => 0 hour 
                    [1] => 2 minute 
                    [2] => 25 secs 
                )

                Array
                (
                    [0] => 0 hour 
                    [1] => 3 minute 
                    [2] => 32 secs 
                )

                Array
                (
                    [0] => 0 hour 
                    [1] => 2 minute 
                    [2] => 51 secs 
                )

1 个答案:

答案 0 :(得分:0)

我真的不明白,但是你想要总结一个数组的所有值吗? $time_in_hour $time_in_minute $time_in_second数组是否只有小时,分钟或秒值?在这种情况下,您可以使用:

// Count all values of all arrays in your array
foreach($mySubArray as $total_time){
    echo array_sum($mySubArray);
}

(感谢halferhis suggestion