2011-12-28 11 views
-4

私はこのような配列を持っています。私は配列のすべての発生に時間の合計を追加する必要があります。それ以下の例であろう午前0時04分03秒+午前0時06分03秒=それはまだ時間であり、追加ように午前0時10分06秒配列時間を合計する方法

Array 
(
    [4894] => Array 
    (
    [0] => Array 
    (
     [Informative] => Array 
     (
     [id] => 109 
    )  
     [jQuery] => Array 
     (
     [length] => 00:04:03 
     [alignment] => 8 
    ) 
    ) 
    [1] => Array 
    (
     [Informative] => Array 
     (
     [id] => 110 
    )  
     [jQuery] => Array 
     (
     [length] => 00:06:03 
     [alignment] => 8 
    ) 
    ) 

がどのように上記配列の長さを追加することができ時間として。

おかげ

+0

あなたは何を試してみましたか?これを解析して秒に変換してからフォーマットするのは簡単なことです。 – jprofitt

答えて

2
$totalTimeSecs = 0; 
foreach ($array as $l1) { // Loop outer array 
    foreach ($l1 as $l2) { // Loop inner arrays 
    if (isset($l2['jQuery']['length'])) { // Check this item has a length 
     list($hours,$mins,$secs) = explode(':',$l2['jQuery']['length']); // Split into H:m:s 
     $totalTimeSecs += (int) ltrim($secs,'0'); // Add seconds to total 
     $totalTimeSecs += ((int) ltrim($mins,'0')) * 60; // Add minutes to total 
     $totalTimeSecs += ((int) ltrim($hours,'0')) * 3600; // Add hours to total 
    } 
    } 
} 
echo "Total seconds: $totalTimeSecs<br />\n"; 

$hours = str_pad(floor($totalTimeSecs/3600),2,'0',STR_PAD_LEFT); 
$mins = str_pad(floor(($totalTimeSecs % 3600)/60),2,'0',STR_PAD_LEFT); 
$secs = str_pad($totalTimeSecs % 60,2,'0',STR_PAD_LEFT); 
echo "Total time string: $hours:$mins:$secs"; 

See it working

0
  1. sum of secondsを見つけ、あなたの配列を反復処理し、各エントリすなわち3600 *時間+ 60 *分+秒の時間からtotal secondsを計算すること。
  2. 時間表現にsum of secondsを変換します
sumSecs = ... 
hour = sumSecs/3600 
min = (sumSecs mod 3600)/60 
sec = sumSecs mod 60 
関連する問題