php 将日期数组排序为年->月

c3frrgcw  于 5个月前  发布在  PHP
关注(0)|答案(2)|浏览(54)

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

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


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

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

yzuktlbb

yzuktlbb1#

在关联数组/Map中按年和按月收集日期。现在,分别进入关联数组中的每个月并usort它们。这样,与对整个数据集进行排序并将日期与甚至不属于同一桶的其他日期进行比较相比,排序成本将是最小的!
最后,在map上执行array_values以删除散列索引(例如用作整数的年或月)。

打喷嚏:

<?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

3zwjbxry

3zwjbxry2#

如果你希望月份数组的关键字是数字索引而不是月份名称,那么你可以通过使用array_values函数重新索引月份数组来实现这一点。
此外,在reserved_dates循环中,您从日期数组访问日期,但reserved_dates的值不可用。
请尝试下面的代码。

<?php
    $reserved_dates = [
    "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",
    "2024-12-08",
    "2024-12-15",
];

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

$reservation = ["reservedDates" => []];

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

    $new = [
        "year" => $year_number,
        "months" => [],
    ];

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

        if (!isset($new["months"][$month_name])) {
            $new["months"][$month_name] = [
                "monthName" => $month_name,
                "dates" => [],
            ];
        }

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

    // re indexing month array
    $new["months"] = array_values($new["months"]);

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

echo "<pre>";
print_r($reservation);
?>

字符串

相关问题