PHP:根据其他数组中定义的带有起始值和结束值的范围将数组切割成碎片

问题描述 投票:2回答:2

我正在为演示室制作预订系统,用户可以在一天内预订房间一段时间(这项工作到目前为止)。预订信息存储在数据库中。预订时间的选择和显示工作正常。

现在我想显示仍然可用的空闲插槽,但我无法知道如何做到这一点。

我正考虑将所有时隙存储到一个数组中,然后根据已经预订的时间将数组切成碎片,大致如下:

//initial values
//all timeslots
array = ["07:00", "07:30", "08:00", "08:30", "09:00", ... , "20:00", "20:30", "21:00", "21:30", "22:00"];
//all booked slots with start and end time
booked1 = ["08:00","09:00"];
booked2 = ["11:00","14:30"];
booked3 = ["18:30","21:00"];
bookedn = ["tt:tt", "tt:tt"];
//How "array" should be seperated and saved into new arrays (start and end times for those ranges could also be possible)
array_new2 = ["07:00", "07:30", "08:00"];
array_new2 = ["09:00", "10:00", "10:30", "11:00"];
array_new3 = ["14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30"];
array_new4 = ["21:00", "21:30", "22:00"];

我还包括2个截图,以显示它看起来如何(1)/应该看起来像(2),以便更好地理解为什么/我需要它。

how it looks without "free slots"

how I want to display the "free slots"

如果有更好/更简单的解决方案,那就更好了!

php arrays
2个回答
0
投票

我没有检查所有的测试用例,但我认为它会按照你的例子进行。

<?php
   $array = ["07:00", "07:30", "08:00", "08:30", "09:00", "09:30",
   "10:00", "10:30", "11:00", "11:30", "12:00", "12:30",
   "13:00", "13:30", "14:00", "14:30", "15:00", "15:30",
   "16:00", "16:30", "17:00", "17:30", "18:00", "18:30",
   "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00"];

  $booked = [
                  ["08:00","09:00"],
                  ["11:00","14:30"],
                  ["18:30","21:00"]
      ];
  $free_slots = [];

  echo '<pre>';
  foreach($booked as $slot){
     $start = 0;
     $index_1 = array_search($slot[0],$array);
     $index_2 = array_search($slot[1],$array);
     $free_slots[] = getSubset($start,$index_1, $index_2);
  }

  if(!empty($array)){
     $free_slots[] = $array;
  }
  print_r($free_slots);

  function getSubset($start, $index_1, $index_2){
      global $array;
      $a = array_slice($array, $start, $index_1+1);
      $array = array_slice($array, $index_2);
      $array = array_values($array);
      return $a;
  }

0
投票

更新:它可能有点脏,但它的工作。你必须稍微改变你的格式。

$all_timeslots = [
    '07:00', '07:30', '08:00', '08:30', '09:00',
    '09:30', '10:00', '10:30', '11:00', '11:30',
    '12:00', '12:30', '13:00', '13:30', '14:00',
    '14:30', '15:00', '15:30', '16:00', '16:30',
    '17:00', '17:30', '18:00', '18:30', '19:00',
    '19:30', '20:00', '20:30', '21:00', '21:30',
    '22:00'
];

$bookeds[] = ['08:00', '09:00'];
$bookeds[] = ['11:00', '14:30'];
$bookeds[] = ['18:30', '21:00'];
$bookeds[] = ['tt:tt', 'tt:tt'];

$available_timeslots = [];
foreach ($bookeds as $key => $booked) {
    $touched = false;

    foreach ($all_timeslots as $timeslot) {
        if ($timeslot == $booked[1]) {
            break;
        }

        if ($touched) {
            array_shift($all_timeslots);
        } else {
            $available_timeslots[$key][] = array_shift($all_timeslots);
        }

        if ($timeslot === $booked[0]) {
            $touched = true;
        }
    }
}

return $available_timeslots;
© www.soinside.com 2019 - 2024. All rights reserved.