dongtingxiao4697 2016-12-06 11:57
浏览 84

在PHP中区分两个多维数组

I have to make some piece of code for scheduling software which shows whether the instructor is available or not.

So I have made the following (month) period:

    $begin = new \DateTime('first day of this month');
    $end = new \DateTime('last day of this month');

    $dailyInterval = new \DateInterval('P1D');
    $dailyPeriod = new \DatePeriod($begin, $dailyInterval, $end);
    $data = [];

    /** @var \DateTime $date */
    foreach($dailyPeriod as $date)
    {
        $currentDate = $date->format('Y-m-d');
        $start = new \DateTime("$currentDate 08:00");
        $finish = new \DateTime("$currentDate 20:00");

        $appointmentInterval = new \DateInterval($appointmentVariant->getInterval());
        $appointmentPeriod = new \DatePeriod($start, $appointmentInterval, $finish);

        /** @var \DateTime $duration */
        foreach($appointmentPeriod as $duration)
        {
            $endDate = clone $duration;
            $data['availability'][$currentDate][] = [
                'start' => $duration->format('H:i:s'),
                'end' => $endDate->modify('+90 minutes')->format('H:i:s')
            ];
        }
    }

The output of this is as follows:

availability: [
    '2016-12-01': [
        0: ['start': '08:00:00', 'end': '09:30:00'],
        1: ['start': '09:30:00', 'end': '11:00:00'],
        2: ['start': '11:00:00', 'end': '12:30:00'],
        3: ['start': '12:30:00', 'end': '14:00:00'],
        4: ['start': '14:00:00', 'end': '15:30:00'],
        5: ['start': '15:30:00', 'end': '17:00:00'],
        6: ['start': '17:00:00', 'end': '18:30:00'],
        7: ['start': '18:30:00', 'end': '20:00:00']
    ]
]

But now we have to get out from the database when instructor is unavailable (this can come from multiple sources, appointments, vacations, recurring events etc), so finally we have the array as the above one, but with unavailability:

unavailability: [
    '2016-12-01': [
        0: ['start': '14:30:00', 'end': '16:30:00'],
        1: ['start': '18:00:00', 'end': '18:20:00'],
    ]
]

And now is the question, how to diff those two arrays to get one final when the instructor is available? It's pretty hard to loop over available dates and times and then unavailable dates and times.

The diff should give me back the following result (DEL = to delete):

availability: [
    '2016-12-01': [
        0: ['start': '08:00:00', 'end': '09:30:00'],
        1: ['start': '09:30:00', 'end': '11:00:00'],
        2: ['start': '11:00:00', 'end': '12:30:00'],
        3: ['start': '12:30:00', 'end': '14:00:00'],
        DEL 4: ['start': '14:00:00', 'end': '15:30:00'],
        DEL 5: ['start': '15:30:00', 'end': '17:00:00'],
        DEL 6: ['start': '17:00:00', 'end': '18:30:00'],
        7: ['start': '18:30:00', 'end': '20:00:00']
    ]
]

Does anybody know the most efficient way to do it?

  • 写回答

2条回答 默认 最新

  • doubaisui2526 2016-12-06 12:24
    关注

    Try splitting the day into 10 minute blocks. You can then unset these depending on the unavailability from the database.

    You could then output the start and end using the min/max values for that day.

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?