dongtu1789 2018-01-31 01:00
浏览 67
已采纳

将Looped DateTime值拉到数组

I have the following code whereby $Clinics is a multidimensional object array in the following format:

Array
(
    [0] => Clinics Object
        (
            [PlanID] => 1
            [id] => 1
            [ClinicCode] => X123ABCD1
            [ClinicDesc] => Test Description
            [SchDay] => Monday
            [SchWeek] => 
            [SchWeekBetween] => 1
        )

    [158] => Clinics Object
        (
            [PlanID] => 1
            [id] => 159
            [ClinicCode] => Y234BCDE2
            [ClinicDesc] => Test Description
            [SchDay] => Monday
            [SchWeek] => 
            [SchWeekBetween] => 1
        )
)

I'm using the below code to loop through each object, starting on the Scheduled Day (SchDay) one a week, and daily if SchDay is daily.

$StartDate = new DateTimeImmutable("2018-04-01");
$EndDate = new DateTimeImmutable("2019-03-31");
$Output = array();

foreach ($Clinics as $Clinic) {

    // Set First Date of loop period
    if ($Clinic->SchDay == "Daily") {
        $ClinicStartDate = $StartDate->format("d-m-Y");
        $SchCount = "+1 day"; 
    } else {
        $ClinicStartDate = "First ".$Clinic->SchDay." of ".$StartDate->format("F Y");
        $SchCount = "+7 days";
    }

    // Loop through Dates and add to array
    for($i = new DateTime($ClinicStartDate); $i <= $EndDate; $i->modify($SchCount)){

        $Clinic->Date = $i;
        $Output[] = $Clinic;    
    } 
};

print_r($Output);

The issue I have is that when I print the array, the Date value is the same for each clinic iteration - the last date of the loop, and the array isn't capturing each DateTime as per the for loop. I think I understand why it is doing this but I can't figure out how to get around it.

  • 写回答

2条回答 默认 最新

  • dongqi1245 2018-01-31 01:27
    关注

    The loop instantiates the object only once. Then you are modifying the object with each iteration and then assigning it's reference to the array. All the assignments reference the same object.

    It's easy to get around this but you'll have to instantiate a new datetime for each assignment:

    for ($i = new DateTime("2018-05-01"); $i <= $EndDate; $i->modify('+7 days')) {
        $Clinic->Date = new DateTime($i->format("Y-m-d"));
        $Output[] = $Clinic;
    }
    

    http://php.net/manual/en/language.oop5.references.php

    EDIT: Here is a working example. All you have to do is copy paste to see it in action.

    <?php
    
    $StartDate = new DateTimeImmutable("2018-04-01");
    $EndDate = new DateTimeImmutable("2019-03-31");
    $Output = array();
    
    for ($i = new DateTime("2018-05-01"); $i <= $EndDate; $i->modify('+7 days')) {
        $Output[] = new DateTime($i->format("Y-m-d"));
    }
    
    var_dump($Output);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?