2013-05-16 109 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