2013-10-07 20 views
3

我有這樣的陣列點心時間日期已經存在或有重複

$date = array{"2013-09-17 00:21:00", 
       "2013-09-23 00:12:00", 
       "2013-09-23 00:41:00", 
       "2013-09-20 00:13:00", 
       "2013-09-19 00:34:00", 
       "2013-09-17 00:38:00"} 

我想總結的是具有相同日期的數組內的時間。

這是我的預期輸出:

$date = array{"2013-09-17 00:59:00", 
       "2013-09-23 00:53:00", 
       "2013-09-20 00:13:00", 
       "2013-09-19 00:34:00"} 

現在,這是我迄今

foreach($date as $key => $value) 
     { 
      $lf_date[] = date("Y-m-d",strtotime($value)); 
      $lf_time[] = date("H:i:s",strtotime($value)); 

      if(isset($lf_date[$key])) 
      { 
       $output[] += $lf_time[$key]; 
      } 
      else 
      { 
       $output[] = $lf_time[$key]; 
      } 

     } 

這給了我嘗試了0輸出T_T ...我已經嘗試過谷歌和它搜索說我必須使用issetarray_key_exists但我不能讓它工作。 。:(感謝的人誰可以幫我

+0

不要隱蔽次日期,但在幾秒鐘 – Voitcus

+1

簡單的問題,做什麼你期望從'「2013-01-01 23:00:00」+「2013-01-01 23:00:00」'?您的數據模型不正確。您正在添加日期和時間段 –

+0

我想你只想得到不同的價值? –

回答

1

用途:

<?php 
$date = array("2013-09-17 00:21:00", 
       "2013-09-23 00:12:00", 
       "2013-09-23 00:41:00", 
       "2013-09-20 00:13:00", 
       "2013-09-19 00:34:00", 
       "2013-09-17 00:38:00"); 

$array = array();    
foreach($date as $key => $value) 
{ 
    $lf_date = date("Y-m-d",strtotime($value)); 
    $lf_time = date("H:i:s",strtotime($value)); 

    $midnight = strtotime("0:00"); 

    if(!isset($array[$lf_date])) 
      $array[$lf_date] = 0;//check is array index exists 

    $array[$lf_date] += strtotime($lf_time) - $midnight; 
} 

foreach($array as $key => $value) 
{ 
    $midnight = strtotime("0:00"); 
    $array[$key] = $key." ".date("G:i:s", $midnight + $value); 
} 

$result = array_values($array); 

print_r($result); 

?> 

輸出:

Array 
(
    [0] => 2013-09-17 0:59:00 
    [1] => 2013-09-23 0:53:00 
    [2] => 2013-09-20 0:13:00 
    [3] => 2013-09-19 0:34:00 
) 
+0

我在這行'$ array [$ lf_date]'上顯示錯誤,它表示非法偏移類型:( – bot

+0

答案已更新,使用'if(!isset($ array [$ lf_date])修復數組索引/偏移量) $ array [$ lf_date] = 0;' – Salim

相關問題