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

PHP日期按年-月分组:将月份数组改为数字索引的技术问题

问题:调整日期分组格式,将月份数组改为数字索引

我在数据库中有一组日期数据,示例如下:

"2023-12-03"
"2023-12-10"
"2023-12-17"
"2024-01-07"
"2024-01-14"
"2024-01-21"

需要返回给前端的格式如下:

[1]=>
array(2) {
  ["year"]=>
  string(4) "2024"
  ["months"]=>
  array(3) {
   [0]=>
    array(2){
        ["monthName"]=>
        string(4) "January"
        ['paid'] = //to be set 
        ["dates"]=>
            array(4) {
            [0]=>
            string(10) "2024-01-07"
            [1]=>
            string(10) "2024-01-14"
            [2]=>
            string(10) "2024-01-21"
            [3]=>
            string(10) "2024-01-28"
            }
    }
}

我已经实现了按年和月对日期排序,但当前月份数组的键是月份名称而非数字索引,相关代码及当前输出如下:

$reserved_dates = array(
    '2023-12-03',
    '2023-12-10',
    '2023-12-17',
    '2023-12-24',
    '2023-12-31',
    '2024-01-07',
    '2024-01-14',
    '2024-01-21',
    '2024-01-28',
    '2024-02-04',
    '2024-02-11',
    '2024-02-18',
    '2024-02-25',
    '2024-12-01',
    '2023-12-03',
    '2024-12-08',
    '2024-12-15',
);

$years = Array();
foreach($reserved_dates as $date) {
    $d = $date['date'];
    list($y,$m) = explode("-",$d);
    $years[$y][] = $d;
}
$years = array_values($years);

foreach($years as $year)
{
    $year_number = date('Y', strtotime($year[0]));

    $new = array(
        "year" => $year_number,
        "months" => array()
    );

    foreach($year as $month)
    {
        $month_name = date('F', strtotime($month));

        if(!isset($new['months'][$month_name]))
        {
            $new['months'][$month_name] = array();
        }

        array_push($new['months'][$month_name], $month);
    }

    array_push($reservation['reservedDates'], $new);
}

当前输出:

array(2) {
    [0]=>
    array(2) {
      ["year"]=>
      string(4) "2023"
      ["months"]=>
      array(1) {
        ["December"]=>
        array(5) {
          [0]=>
          string(10) "2023-12-03"
          [1]=>
          string(10) "2023-12-10"
          [2]=>
          string(10) "2023-12-17"
          [3]=>
          string(10) "2023-12-24"
          [4]=>
          string(10) "2023-12-31"
        }
      }
    }
}

请协助修改代码,将月份数组的键改为数字索引,适配前端要求的格式。


解决方案

核心改动是调整月份分组的存储逻辑:不再用月份名称作为数组键,而是先按月份数字临时分组,最后转换为带指定字段的数字索引数组,完全匹配前端格式要求。

修改后的完整代码:

$reserved_dates = array(
    '2023-12-03',
    '2023-12-10',
    '2023-12-17',
    '2023-12-24',
    '2023-12-31',
    '2024-01-07',
    '2024-01-14',
    '2024-01-21',
    '2024-01-28',
    '2024-02-04',
    '2024-02-11',
    '2024-02-18',
    '2024-02-25',
    '2024-12-01',
    '2023-12-03',
    '2024-12-08',
    '2024-12-15',
);

// 初始化结果容器
$reservation['reservedDates'] = [];

// 第一步:按年份+月份数字分组日期
$yearGroups = [];
foreach ($reserved_dates as $dateStr) {
    // 修复原代码错误:直接使用字符串日期,而非数组取值
    list($year, $monthNum) = explode("-", $dateStr);
    $yearGroups[$year][$monthNum][] = $dateStr;
}

// 第二步:转换为前端要求的格式
foreach ($yearGroups as $year => $monthGroups) {
    // 按月份数字排序,保证月份顺序正确
    ksort($monthGroups);
    
    $yearItem = [
        "year" => $year,
        "months" => []
    ];
    
    foreach ($monthGroups as $monthNum => $dates) {
        // 去重重复日期,排序日期
        $uniqueDates = array_unique($dates);
        sort($uniqueDates);
        
        // 组装单个月份的结构
        $monthItem = [
            "monthName" => date('F', strtotime("{$year}-{$monthNum}-01")),
            "paid" => false, // 可根据业务逻辑设置实际值
            "dates" => $uniqueDates
        ];
        
        // 追加到月份数组,自动生成数字索引
        $yearItem['months'][] = $monthItem;
    }
    
    $reservation['reservedDates'][] = $yearItem;
}

// 输出结果
print_r($reservation['reservedDates']);

关键改动说明

  1. 修复原代码错误:原代码中$d = $date['date']逻辑错误,因为$reserved_dates是字符串数组,直接使用循环变量即可。
  2. 按月份数字分组:改用月份数字(如12代表12月)分组,后续通过ksort保证月份按自然顺序排列。
  3. 匹配前端结构:每个月份项直接组装成包含monthName、paid、dates的数组,通过[]追加到months数组,自动生成数字索引。
  4. 数据优化:对重复日期去重,对每个月的日期排序,保证数据整洁。

修改后的输出会完全符合前端要求的格式,months数组使用数字索引,且包含指定字段。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 10:08:14