简体   繁体   English

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

[英]How to determine if each date in an array is between dates in other arrays

I have an array with dates. 我有一个日期数组。 I have to determine which dates belongs to which periods. 我必须确定哪个日期属于哪个时期。 For that purpose I have two other arrays - one with start dates and one with end dates for these periods. 为此,我有另外两个数组-这些周期的一个带有开始日期,另一个带有结束日期。

I have tried foreach loops for the dates and the DatePeriod class but I couldn't get it to work. 我已经尝试过日期和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 (extract) / $dates 日期(提取)/ $ dates

$dates = Array ( [0] => 2011-04-11 
                [1] => 2011-06-28 
                [2] => 2011-09-26 
                [3] => 2012-01-02 
                [4] => 2012-05-12 )

start dates with assigned keys (extract) / $startdates 使用分配的键的开始日期(提取)/ $ startdates

Array ( [10] => 2011-01-01 
        [20] => 2011-07-01 
        [30] => 2012-01-01 
        [40] => 2012-07-01 )

end dates with assigned keys (extract) / $enddates 分配了键的结束日期(摘录)/ $ enddates

Array ( [10] => 2011-06-30 
        [20] => 2011-12-31 
        [30] => 2012-06-30 
        [40] => 2012-12-31 )

I would like the result to be a new array, where the dates in the $dates array becomes keys and the periods in the start- and end dates arrays becomes values like this: 我希望结果是一个新数组,其中$ dates数组中的日期成为键,开始日期和结束日期数组中的句点变为如下值:

Array ( [2011-04-11] => 10 
        [2011-06-28] => 10 
        [2011-09-26] => 20 
        [2012-01-02] => 30 
        [2012-05-12] => 30 )

You can loop around your main dates variable for the period of start and end date arrays, 您可以在主日期变量中循环使用开始日期和结束日期数组的周期,

// combining keys and values
$temp   = array_combine($startdates, $enddates);
$result = [];
// & for making changes on assigned address of variable regardless of array_walk function scope
array_walk($dates, function ($item, $key) use (&$result, $temp, $startdates) { 
    foreach ($temp as $k => $v) {
        // comparing with start and end date range
        if (strtotime($item) >= strtotime($k) && strtotime($item) <= strtotime($v)) {
            // searching by value and getting key
            $result[$item] = array_search($k, $startdates); 
            // if comes to this loop break as its never gonna come here
            break;
        }
    }
});

array_combine — Creates an array by using one array for keys and another for its values array_combine —通过使用一个数组作为键并使用另一个数组作为其值来创建数组
array_walk — Apply a user supplied function to every member of an array array_walk —将用户提供的函数应用于数组的每个成员
array_search — Searches the array for a given value and returns the first corresponding key if successful array_search —在数组中搜索给定值,如果成功,则返回第一个对应的键

Output 输出量

Array
(
    [2011-04-11] => 10
    [2011-06-28] => 10
    [2011-09-26] => 20
    [2012-01-02] => 30
    [2012-05-12] => 30
)

Demo . 演示

Because you are comparing Ymd formatted dates, you don't need date/time functions -- in other words, just treat the dates as strings. 因为要比较Ymd格式的日期,所以不需要日期/时间函数-换句话说,只需将日期视为字符串即可。

Simultaneously iterate the start and end elements. 同时迭代开始和结束元素。 When you find the correct range, store the data and continue with the next date to assess. 找到正确的范围后,请存储数据并继续进行下一个评估日期。

Code: ( Demo ) 代码:( 演示

$dates = ['2011-04-11', '2011-06-28', '2011-09-26', '2012-01-02', '2012-05-12'];
$startdates = [
    10 => '2011-01-01',
    20 => '2011-07-01',
    30 => '2012-01-01',
    40 => '2012-07-01'
];
$enddates = [
    10 => '2011-06-30',
    20 => '2011-12-31',
    30 => '2012-06-30',
    40 => '2012-12-31'
];

foreach ($dates as $date) {
    foreach ($startdates as $key => $startdate) {
        if ($date >= $startdate && $date <= $enddates[$key]) {
            $result[$date] = $key;
            continue 2;
        }
    }
    $result[$date] = 'out of bounds';
}
var_export($result);

Output: 输出:

array (
  '2011-04-11' => 10,
  '2011-06-28' => 10,
  '2011-09-26' => 20,
  '2012-01-02' => 30,
  '2012-05-12' => 30,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM