猿问

如何确定数组中的每个日期是否在其他数组中的日期之间

我有一个日期数组。我必须确定哪个日期属于哪个时期。为此,我有另外两个数组-这些周期的一个带有开始日期,另一个带有结束日期。


我尝试了日期和DatePeriod类的foreach循环,但无法正常工作。


foreach ($dates as $value) {

  foreach ($startdates as $key => $value1) {

    foreach ($enddates as $key => $value2) {

      if ($value > $value1 && $value < $value2) {

        result[$value] = $key;

      }

    }

  }

}

日期(提取)/ $ dates


$dates = Array ( [0] => 2011-04-11 

                [1] => 2011-06-28 

                [2] => 2011-09-26 

                [3] => 2012-01-02 

                [4] => 2012-05-12 )

使用分配的键的开始日期(提取)/ $ startdates


Array ( [10] => 2011-01-01 

        [20] => 2011-07-01 

        [30] => 2012-01-01 

        [40] => 2012-07-01 )

分配了键的结束日期(摘录)/ $ enddates


Array ( [10] => 2011-06-30 

        [20] => 2011-12-31 

        [30] => 2012-06-30 

        [40] => 2012-12-31 )

我希望结果是一个新数组,其中$ dates数组中的日期成为键,开始日期和结束日期数组中的句点变为如下值:


Array ( [2011-04-11] => 10 

        [2011-06-28] => 10 

        [2011-09-26] => 20 

        [2012-01-02] => 30 

        [2012-05-12] => 30 )


手掌心
浏览 175回答 2
2回答

波斯汪

因为您正在比较Ymd格式的日期,所以不需要日期/时间函数-换句话说,只需将日期视为字符串即可。同时迭代开始和结束元素。当您找到正确的范围时,请存储数据并继续进行下一个评估日期。代码:(演示)$dates = ['2011-04-11', '2011-06-28', '2011-09-26', '2012-01-02', '2012-05-12'];$startdates = [&nbsp; &nbsp; 10 => '2011-01-01',&nbsp; &nbsp; 20 => '2011-07-01',&nbsp; &nbsp; 30 => '2012-01-01',&nbsp; &nbsp; 40 => '2012-07-01'];$enddates = [&nbsp; &nbsp; 10 => '2011-06-30',&nbsp; &nbsp; 20 => '2011-12-31',&nbsp; &nbsp; 30 => '2012-06-30',&nbsp; &nbsp; 40 => '2012-12-31'];foreach ($dates as $date) {&nbsp; &nbsp; foreach ($startdates as $key => $startdate) {&nbsp; &nbsp; &nbsp; &nbsp; if ($date >= $startdate && $date <= $enddates[$key]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$date] = $key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue 2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $result[$date] = 'out of bounds';}var_export($result);输出:array (&nbsp; '2011-04-11' => 10,&nbsp; '2011-06-28' => 10,&nbsp; '2011-09-26' => 20,&nbsp; '2012-01-02' => 30,&nbsp; '2012-05-12' => 30,)

精慕HU

您可以在主日期变量中循环使用开始日期和结束日期数组的时间段,// combining keys and values$temp&nbsp; &nbsp;= array_combine($startdates, $enddates);$result = [];// & for making changes on assigned address of variable regardless of array_walk function scopearray_walk($dates, function ($item, $key) use (&$result, $temp, $startdates) {&nbsp;&nbsp; &nbsp; foreach ($temp as $k => $v) {&nbsp; &nbsp; &nbsp; &nbsp; // comparing with start and end date range&nbsp; &nbsp; &nbsp; &nbsp; if (strtotime($item) >= strtotime($k) && strtotime($item) <= strtotime($v)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // searching by value and getting key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$item] = array_search($k, $startdates);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if comes to this loop break as its never gonna come here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});array_combine&nbsp;—通过使用一个数组作为键并使用另一个数组作为其值来创建数组array_walk&nbsp;—将用户提供的函数应用于数组的每个成员array_search&nbsp;—搜索数组以获取给定值,如果成功则返回第一个对应的键输出Array(&nbsp; &nbsp; [2011-04-11] => 10&nbsp; &nbsp; [2011-06-28] => 10&nbsp; &nbsp; [2011-09-26] => 20&nbsp; &nbsp; [2012-01-02] => 30&nbsp; &nbsp; [2012-05-12] => 30)演示。
随时随地看视频慕课网APP
我要回答