二维数组的重复行来表示该行的开始日期和结束日期值中的每个日期

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

我有以下名为 $exhibitions 的数组:

Array
(
    [0] => Array
        (
            [exhibition_title] => Picasso
            [venue_name] => Gallery 1
            [room_name] => Room 4
            [start_date] => 2023-11-15
            [end_date] => 2023-12-10
            [room_id] => 261
            [exhibition_id] => 3540
            [venue_order] => 2
        )

    [1] => Array
        (
            [exhibition_title] => Monet
            [venue_name] => Gallery 4
            [room_name] => Room 2
            [start_date] => 2023-10-01
            [end_date] => 2023-11-13
            [room_id] => 274
            [exhibition_id] => 8417
            [venue_order] => 1
        )
)

我需要做的是使用

start_date
end_date
创建每个展览开放的一系列日期,然后对于每个日期,基本上将现有子数组中的其余信息与个人复制日期附加到子数组的末尾。我想要的输出是:

Array
(
    [0] => Array
        (
            [exhibition_title] => Picasso
            [venue_name] => Gallery 1
            [room_name] => Room 4
            [start_date] => 2023-11-15
            [end_date] => 2023-12-10
            [room_id] => 261
            [exhibition_id] => 3540
            [venue_order] => 2
            [date] => 2023-11-15
        )
    [1] => Array
        (
            [exhibition_title] => Picasso
            [venue_name] => Gallery 1
            [room_name] => Room 4
            [start_date] => 2023-11-15
            [end_date] => 2023-12-10
            [room_id] => 261
            [exhibition_id] => 3540
            [venue_order] => 2
            [date] => 2023-11-16
        )
    [2] => Array
        (
            [exhibition_title] => Picasso
            [venue_name] => Gallery 1
            [room_name] => Room 4
            [start_date] => 2023-11-15
            [end_date] => 2023-12-10
            [room_id] => 261
            [exhibition_id] => 3540
            [venue_order] => 2
            [date] => 2023-11-17
        )
    [... etc. for the rest of the dates in the Picasso date range]

    [25] => Array
        (
            [exhibition_title] => Monet
            [venue_name] => Gallery 4
            [room_name] => Room 2
            [start_date] => 2023-10-01
            [end_date] => 2023-11-13
            [room_id] => 274
            [exhibition_id] => 8417
            [venue_order] => 1
            [date] => 2023-10-01
        )

    [26] => Array
        (
            [exhibition_title] => Monet
            [venue_name] => Gallery 4
            [room_name] => Room 2
            [start_date] => 2023-10-01
            [end_date] => 2023-11-13
            [room_id] => 274
            [exhibition_id] => 8417
            [venue_order] => 1
            [date] => 2023-10-02
        )

    [27] => Array
        (
            [exhibition_title] => Monet
            [venue_name] => Gallery 4
            [room_name] => Room 2
            [start_date] => 2023-10-01
            [end_date] => 2023-11-13
            [room_id] => 274
            [exhibition_id] => 8417
            [venue_order] => 1
            [date] => 2023-10-03
        )
    [... etc. for the rest of the dates in the Monet date range]
)

我看到了这个类似的帖子并对其进行了修改,以便将其添加到名为“日期”的新列中的数组中,而不是回显

$given_date

foreach ($exhibitions as $key => $value) {

    $startDate = strtotime($value['start_date']);
    $endDate = strtotime($value['end_date']);

    // Loop between timestamps, 24 hours at a time
    for ($i = $startDate; $i <= $endDate; $i = $i + 86400 ) {
            $given_date = date('Y-m-d', $i );     
            $exhibitions[$key]['date'] = $given_date;
    }    

};

问题在于,它不是为日期范围中的每个日期添加一个日期,而是仅添加该范围中的最后一个日期。当我回显

$given_date
只是为了确保它迭代每个日期时,它列出了范围内的每个日期,所以我假设我添加新列的方式有问题,但我不知道是什么。

php arrays datetime range repeat
2个回答
1
投票

您的代码中有一个小错误。您将在循环的每次迭代中覆盖 'date' 键,因此只有最后一个日期存储在原始数组中。我已经为你重写了代码,请看一下,希望它能解决你的问题。

<?php

$exhibitions = array(
    array(
        'exhibition_title' => 'Picasso',
        'venue_name' => 'Gallery 1',
        'room_name' => 'Room 4',
        'start_date' => '2023-11-15',
        'end_date' => '2023-12-10',
        'room_id' => 261,
        'exhibition_id' => 3540,
        'venue_order' => 2,
    ),
    array(
        'exhibition_title' => 'Monet',
        'venue_name' => 'Gallery 4',
        'room_name' => 'Room 2',
        'start_date' => '2023-10-01',
        'end_date' => '2023-11-13',
        'room_id' => 274,
        'exhibition_id' => 8417,
        'venue_order' => 1,
    )
);

$result = array();

foreach ($exhibitions as $key => $value) {
    $startDate = strtotime($value['start_date']);
    $endDate = strtotime($value['end_date']);

    // Loop between timestamps, 24 hours at a time
    for ($i = $startDate; $i <= $endDate; $i = $i + 86400) {
        $given_date = date('Y-m-d', $i);

        // Create a new array for each date and append it to the result array
        $result[] = array(
            'exhibition_title' => $value['exhibition_title'],
            'venue_name' => $value['venue_name'],
            'room_name' => $value['room_name'],
            'start_date' => $value['start_date'],
            'end_date' => $value['end_date'],
            'room_id' => $value['room_id'],
            'exhibition_id' => $value['exhibition_id'],
            'venue_order' => $value['venue_order'],
            'date' => $given_date,
        );
    }
}

// $result now contains the desired output
print_r($result);

?>

您可以检查此链接上的输出https://onecompiler.com/php/3ztkkmvw9


0
投票
  • 迭代输入数组的行并访问开始日期和结束日期。
  • 使用 PHP 的本机 DateTime 方法创建可迭代的日期有效负载。
  • 循环这些日期,将单个日期附加到行数据中,然后将扩展行传递到结果数组中。

要具有“包含”日期范围,请在结束日期上添加 1 天(否则每个范围都会短一天)。

通过使用联合运算符 (

+
) 将生成的日期附加到行数据,您可以避免在推送时需要写出行中的每个元素。

代码:(演示

$result = [];
foreach ($exhibitions as $row) {
    $dateRange = new DatePeriod(
        new DateTime($row['start_date']),
        new DateInterval('P1D'),
        new DateTime("{$row['end_date']} +1 day")
    );
    foreach ($dateRange as $d) {
        $result[] = $row + ['date' => $d->format("Y-m-d")];
    }
}
var_export($result);
© www.soinside.com 2019 - 2024. All rights reserved.