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

问题描述 投票:0回答:1

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

例如

"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"
        }
      }
    }
php arrays sorting date
1个回答
0
投票

如果您希望月份数组键是数字索引而不是月份名称,那么您可以通过使用 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",
    "2023-12-03",
    "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] = [];
        }

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

    // Reindexing the months array
    $new["months"] = array_values($new["months"]);

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

echo "<pre>";
print_r($reservation);
?>
© www.soinside.com 2019 - 2024. All rights reserved.