You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于周期构建多维数组:规划器空闲月份分组实现

按预订月份分割空闲时段多维数组实现方案

当前已实现生成排除预订月份(示例中为2月、8月)的一维空闲时段数组,需要将其改造为多维数组,以预订月份为分隔点,把预订月份之前、相邻预订月份之间、最后一个预订月份之后的空闲时段分别放入独立子数组。

完整实现代码

<?php
$free_time_frame = [];

$start = new DateTime("2022-01-01");
$end = new DateTime("2022-12-31");

$interval = new DateInterval("P1M"); // 1个月间隔
$period = new DatePeriod($start, $interval, $end);
$seasons = [8,2];
// 对预订月份按升序排序,确保按时间顺序处理分隔点
sort($seasons);

foreach ($period as $date) {
    $monthNum = (int)$date->format("n");
    if (in_array($monthNum, $seasons)) {
        continue;
    }

    $start_date = new DateTime($date->format("Y-m-01"));
    $end_date = new DateTime($date->format("Y-m-t"));
    $diff = $start_date->diff($end_date);
    $days = $diff->days + 1;

    $free_time_frame[] = [
        "from" => $start_date->format("Y-m-d"),
        "to" => $end_date->format("Y-m-d"),
        "frame" => $days,
        "season" => $monthNum
    ];
}

// 分组处理逻辑
$groupedResult = [];
$currentGroupIndex = 0;
$seasonIndex = 0;
$totalSeasons = count($seasons);

// 初始化第一个分组
$groupedResult[$currentGroupIndex] = [];

foreach ($free_time_frame as $item) {
    $currentMonth = $item['season'];
    // 当前月份超过当前分隔月份时,创建新分组
    if ($seasonIndex < $totalSeasons && $currentMonth > $seasons[$seasonIndex]) {
        $currentGroupIndex++;
        $groupedResult[$currentGroupIndex] = [];
        $seasonIndex++;
    }
    $groupedResult[$currentGroupIndex][] = $item;
}

// 输出格式化后的结果
echo json_encode($groupedResult, JSON_PRETTY_PRINT);
?>

关键步骤说明

  1. 排序预订月份:对$seasons数组升序排序,确保按时间顺序处理分隔点(示例中变为[2,8])。
  2. 完善字段:生成一维数组时补充season字段(当前月份编号),并将原slot字段替换为需求中的frame。
  3. 分组逻辑:
    • 初始化分组数组与索引变量
    • 遍历每个空闲时段条目,判断当前月份是否超过当前分隔月份,若超过则创建新子数组并切换分组索引
    • 将当前条目加入对应分组

预期输出

[
   [
      {
         "from":"2022-01-01",
         "to":"2022-01-31",
         "frame":31,
         "season":1
      }
   ],
   [
      {
         "from":"2022-03-01",
         "to":"2022-03-31",
         "frame":31,
         "season":3
      },
      {
         "from":"2022-04-01",
         "to":"2022-04-30",
         "frame":30,
         "season":4
      },
      {
         "from":"2022-05-01",
         "to":"2022-05-31",
         "frame":31,
         "season":5
      },
      {
         "from":"2022-06-01",
         "to":"2022-06-30",
         "frame":30,
         "season":6
      },
      {
         "from":"2022-07-01",
         "to":"2022-07-31",
         "frame":31,
         "season":7
      }
   ],
   [
      {
         "from":"2022-09-01",
         "to":"2022-09-30",
         "frame":30,
         "season":9
      },
      {
         "from":"2022-10-01",
         "to":"2022-10-31",
         "frame":31,
         "season":10
      },
      {
         "from":"2022-11-01",
         "to":"2022-11-30",
         "frame":30,
         "season":11
      },
      {
         "from":"2022-12-01",
         "to":"2022-12-31",
         "frame":31,
         "season":12
      }
   ]
]

内容的提问来源于stack exchange,提问作者user20718909

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 22:15:27