嵌套的foreach增加两个索引/检查日期是否在2个数组之间

有没有办法同时增加两个foreach循环?

我有2个数组:

$datum = new DateTime("now");
$jaar = date("y",strtotime('now'));
$maand = strftime("%B");

$vakanties_start = array(
    "BeginKerst"       =>   new DateTime("$jaar-01-01"),
    "Voorjaar"         =>   new DateTime("$jaar-02-22"),
    "Mei"              =>   new DateTime("$jaar-04-25"),
    "Zomer"            =>   new DateTime("$jaar-07-18"),
    "EindKerst"        =>   new DateTime("$jaar-12-21"),
    "Test"             =>   new DateTime("$jaar-05-20")
);


$vakanties_eind = array(
    "BeginKerst"       =>   new DateTime("$jaar-01-05"),
    "Voorjaar"         =>   new DateTime("$jaar-03-01"),
    "Mei"              =>   new DateTime("$jaar-05-03"),
    "Zomer"            =>   new DateTime("$jaar-08-30"),
    "EindKerst"        =>   new DateTime("$jaar-12-31"),
    "Test"             =>   new DateTime("$jaar-05-25")
);

假期的开始和假期的结束。我需要检查今天是否在那些日子之间。

我试图用2 foreach循环来检查它,但是比我将得到此错误: 开始休假:1-1 假期结束:5-1

开始:1-1 结束:3-1

等等。这显然不是我想要的方式,因为这意味着在第一个foreach结束时,假期在1-1和31-12之间。

我尝试过的另一件事是像这样验证数组的键:

    foreach ($vakanties_start as $vakantie_start){
        foreach ($vakanties_eind as $vakantie_eind){
            if ($vakantie_start->format('Y-m-d') >= $datum->format('Y-m-d') && $vakantie_eind->format('Y-m-d') <= $datum->format('Y-m-d')){
                if (key($vakantie_start) == key($vakantie_eind)){

                    echo $vakantie_start->format('Y-m-d') . "<br>" . key($vakanties_start) . "<br>";
                    echo $vakantie_eind->format('Y-m-d') . "<br>" . key($vakanties_eind) . "<br>";
                }
             }
        }
    }

但这会使代码仅显示1-1至5-1日期。有人可以指引我正确的方向吗?也许我做错了,但我不知道该怎么做

评论
  • zipsum
    zipsum 回复

    You can use the $key => $value version of a foreach statement on $vakanties_start and use that key to access the corresponding value in $vakanties_eind:

    $datum = (new DateTime("now"))->format('Y-m-d');
    $jaar = date("y",strtotime('now'));
    $maand = strftime("%B");
    
    $vakanties_start = array(
        "BeginKerst"       =>   new DateTime("$jaar-01-01"),
        "Voorjaar"         =>   new DateTime("$jaar-02-22"),
        "Mei"              =>   new DateTime("$jaar-04-25"),
        "Zomer"            =>   new DateTime("$jaar-07-18"),
        "EindKerst"        =>   new DateTime("$jaar-12-21"),
        "Test"             =>   new DateTime("$jaar-05-20")
    );
    
    
    $vakanties_eind = array(
        "BeginKerst"       =>   new DateTime("$jaar-01-05"),
        "Voorjaar"         =>   new DateTime("$jaar-03-01"),
        "Mei"              =>   new DateTime("$jaar-05-03"),
        "Zomer"            =>   new DateTime("$jaar-08-30"),
        "EindKerst"        =>   new DateTime("$jaar-12-31"),
        "Test"             =>   new DateTime("$jaar-05-25")
    );
    
    foreach ($vakanties_start as $key => $vakantie_start) {
        $vakantie_eind = $vakanties_eind[$key];
        if ($vakantie_start->format('Y-m-d') <= $datum && $vakantie_eind->format('Y-m-d') >= $datum){
            echo $vakantie_start->format('Y-m-d') . "<br>" . key($vakanties_start) . "<br>";
            echo $vakantie_eind->format('Y-m-d') . "<br>" . key($vakanties_eind) . "<br>";
        }
    }
    

    产出(截至2020-05-24):

    2020-05-20<br>BeginKerst<br>2020-05-25<br>BeginKerst<br>
    

    Note that you had an error in your code, you were comparing for start>=today && today>=end where you should have start<=today && today<=end. Also it's more efficient to make $datum formatted from the beginning instead of formatting it every time through the loop.

    Demo on 3v4l.org

  • laut
    laut 回复

    您在正确的轨道上。我更改了您组织数据的方式,因此遍历数据更加简单。

    I also used DateTime() objects in my comparison because they are comparable. No need to format them as a string first.

    $jaar = date('Y');
    $vakanties = [
        'BeginKerst' => [
            'start' => new DateTime("$jaar-01-01"),
            'end'   => new DateTime("$jaar-01-05"),
        ],
        'Voorjaar' => [
            'start' => new DateTime("$jaar-02-22"),
            'end'   => new DateTime("$jaar-03-01"),
        ],
        'Mei' => [
            'start' => new DateTime("$jaar-04-25"),
            'end'   => new DateTime("$jaar-05-03"),
        ],
        'Zomer' => [
            'start' => new DateTime("$jaar-07-18"),
            'end'   => new DateTime("$jaar-08-30"),
        ],
        'EindKerst' => [
            'start' => new DateTime("$jaar-12-21"),
            'end'   => new DateTime("$jaar-12-31"),
        ],
        'Test' => [
            'start' =>  new DateTime("$jaar-05-20"),
            'end'   => new DateTime("$jaar-05-25"),
        ],
    ];
    
    $now = new DateTime();
    foreach ($vakanties as $name => $vakanty) {
        if ($now >= $vakanty['start'] && $now <= $vakanty['end']) {
            echo "today is in the middle of a vacation: " . $name;
        }
    }
    

    输出量

    today is in the middle of a vacation: Test
    

    Demo