duanji5116 2013-05-16 00:09
浏览 19
已采纳

匹配两个不同的数组并将不匹配的值存储在第三个数组中

I have two arrays :

 $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
)

now $data is my blocked time slots. and $slot is my total time slots. i want to match blocked time slots against total slots and get the available time slots.

I am trying through this loops but its not working.

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]));       
            }                       
         }
     }
 }

But its giving the result twice. and not filtering the busy records properly.

Can anyone please help me to find out my mistake and get the correct available time slots ?

  • 写回答

1条回答 默认 最新

  • duangua6912 2013-05-16 00:16
    关注
    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]));
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用