我的数据库表中有一个日期列表

例如:

"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"
            }
    }

我已经成功地按年和月对日期进行了排序,但我的问题是,Monters数组键是月份名称,而不是编号索引:

    $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"
        }
      }
    }

推荐答案

在关联数组/映射中按年和按月收集日期.现在,分别进入关联数组中的每个月,并将其设置为usort.这样,与对整个数据集进行排序并将日期与甚至不属于同一个桶的其他日期进行比较相比,排序成本将是最小的!

最后,在map上执行array_values,以删除哈希索引(如年或月用作整数).

Snippet:

<?php

$data = [];

foreach($reserved_dates as $d){
    list($year, $month) = explode("-", $d);
    $data[ $year ]['year'] = $year;
    $data[ $year ]['months'][ $month ] = $data[ $year ]['months'][ $month ] ?? [];
    $data[ $year ]['months'][ $month ]['monthName'] = date("F", strtotime($d));
    $data[ $year ]['months'][ $month ]['paid'] = true; // or false whatever
    $data[ $year ]['months'][ $month ]['dates'] = $data[ $year ]['months'][ $month ]['dates'] ?? [];
    $data[ $year ]['months'][ $month ]['dates'][] = $d;
}


foreach($data as &$months){
    foreach($months['months'] as &$each_month){
        usort($each_month['dates'], fn($a, $b) => DateTime::createFromFormat('!Y-m-d', $a) <=> DateTime::createFromFormat('!Y-m-d', $b));
    }
    $months['months'] = array_values($months['months']);
}

$data = array_values($data);

print_r($data);

Live Demo

Php相关问答推荐

无法在Laravel/Vue停靠容器中启动MySQL

WooCommerce/WordPress:简单的产品属性显示

添加登录:需要app.yaml仍然允许所有拥有Google帐户的人访问

在WooCommerce中的本地收件发货方式下添加一个 Select 字段

将SVG包含在另一个php生成的SVG中

WooCommerce短代码,显示特定产品的购物车徽章中的当前数量

在WooCommerce存档页面中显示可变产品的库存变化属性

Woocommerce单一产品Ajax添加到带有自定义购物车项目数据的购物车

在WooCommerce购物车页面应用优惠券不会';t刷新总计

如何在其他数组php中创建具有关联数组的数组

在具有Angular 的 Laravel 应用程序中,不显示图像

WooCommerce 以编程方式添加的费用不会持续存在

Woocommerce 中为空时隐藏显示变体自定义字段数据

添加产品标签以通过 WooCommerce 邮箱通知订购商品

WooCommerce 有关新订单的附加邮箱通知,其中包含具体详细信息

我需要一个正则表达式模式来获取在不包含特定字符串后面的模式中的文本

如何从 PHP 中的字符串位置排除图像扩展名

构建 [Controller] 时目标 [Interface] 不可实例化

PHP Remedy API 调用以创建附件无效的条目(使用 Postman Works!)

我正在使用 Baryryvdh/laravel-snappy 从 HTML 导出 PDF 它正在工作但缩小了 pdf 文件