2013-05-16 106 views
0

我有兩個陣列:匹配兩個不同的陣列和不匹配的值存儲在第三陣列

$data = Array 
(
[0] => Array 
    (
     [StartTime] => 13:00:00 
     [EndTime] => 14:00:00 
    ) 

[1] => Array 
    (
     [StartTime] => 16:00:00 
     [EndTime] => 16:30:00 
    ) 
) 

$slot = Array 
(
    [0] => 09:00:00 
    [1] => 09:30:00 
    [2] => 10:00:00 
    [3] => 10:30:00 
    [4] => 11:00:00 
    [5] => 11:30:00 
    [6] => 12:00:00 
    [7] => 12:30:00 
    [8] => 13:00:00 
    [9] => 13:30:00 
    [10] => 14:00:00 
    [11] => 14:30:00 
    [12] => 15:00:00 
    [13] => 15:30:00 
    [14] => 16:00:00 
    [15] => 16:30:00 
    [16] => 17:00:00 
    [17] => 17:30:00 
) 

現在$數據是我的阻塞時隙。 $ slot是我的總時段。我想匹配總時隙中的阻塞時隙並獲得可用時隙。

我想通過這個循環,但它不工作。

for($i=0;$i<count($slot)-1;$i++)      
{ 
    if(count($data)==0) 
    { 
     $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));   
    } 
    else 
    { 
      for($j=0;$j<count($data);$j++) 
      {  
      if(strtotime($slot[$i]) >= strtotime($data[$j]['StartTime']) && strtotime($slot[$i]) <= strtotime($data[$j]['EndTime'])) 
      { 
      //echo "busy slot so it should not be in result."; 
      } 
      else 
      { 
      $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));  
      }      
     } 
    } 
} 

但它給出的結果兩次。而不是正確過濾繁忙的記錄。

任何人都可以請幫我找出我的錯誤,並獲得正確的可用時間段?

回答

0
for($i=0;$i<count($slot)-1;$i++) 
{ 
    if(count($data)==0) 
    { 
     $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1]));   
    } 
    else 
    { 
     $free = true; 
     for($j=0;$j<count($data);$j++) 
     {  
      if(strtotime($slot[$i]) >= strtotime($data[$j]['StartTime']) && strtotime($slot[$i]) <= strtotime($data[$j]['EndTime'])) 
      { 
        $free = false; 
        break; // Not necessary to check the rest 
      } 

     } 
     if ($free) { 
      $result[] = date('H:i',strtotime($slot[$i]))."-".date('H:i',strtotime($slot[$i+1])); 
     } 
    } 
} 
+0

哇..這就是工作。我正在檢查所有的插槽一次又一次。那是我的錯誤。非常感謝Pavel。 – Mausami