比较两组数组日期,并在PHP中找到最接近的匹配日期的键值


Comparing two sets of array dates, and finding the closest key value of matching dates in PHP

看起来很复杂的问题,但我会尽可能简单地解释:

我有两个数组,A和b。

数组A包含测量的日期。

数组B包含在测量中注意到错误的日期。

我目前将数组A作为图形(jpgraph)上的X轴,并根据关键值绘制点作为标准差,以确定在不同日期的测量结果。

我想做的是在图表中添加标记,在最接近数组b中的日期[键值]。

基本上,我想找到数组A中数组B中每个条目的最近日期,并输出数组A中的键值。

希望你能理解这个问题,并提供一些帮助-这一直困扰着我一段时间。

编辑:数组如下所示:

数组:

Array ( [0] => 2013-03-12 [1] => 2013-03-26 [2] => 2013-04-09 [3] => 2013-05-01 [4] => 2013-05-28 [5] => 2013-06-11 [6] => 2013-06-25 [7] => 2013-07-16 [8] => 2013-07-31 [9] => 2013-08-13 [10] => 2013-08-27 [11] => 2013-09-10 [12] => 2013-09-24 [13] => 2013-10-15 [14] => 2013-10-30 [15] => 2013-11-12 [16] => 2013-11-26 [17] => 2013-12-10 [18] => 2013-12-17 [19] => 2014-01-14 [20] => 2014-01-29 [21] => 2014-02-11 [22] => 2014-02-25 [23] => 2014-03-11 [24] => 2014-03-25 [25] => 2014-04-15 [26] => 2014-04-30 [27] => 2014-05-13 [28] => 2014-05-27 [29] => 2014-06-10 [30] => 2014-06-24 [31] => 2014-07-15 [32] => 2014-08-12 [33] => 2014-08-26 [34] => 2014-09-23 [35] => 2014-10-14 [36] => 2014-10-29 [37] => 2014-11-11 [38] => 2014-11-25 [39] => 2014-12-16 [40] => 2015-01-06 [41] => 2015-01-20 [42] => 2015-02-03 [43] => 2015-02-17 [44] => 2015-03-03 [45] => 2015-03-17 [46] => 2015-04-21 [47] => 2015-05-05 [48] => 2015-05-19 [49] => 2015-06-02 [50] => 2015-06-16 [51] => 2015-07-07 [52] => 2015-07-21 [53] => 2015-08-04 [54] => 2015-08-18 [55] => 2015-09-01 [56] => 2015-09-15 [57] => 2015-09-29 [58] => 2015-10-13 [59] => 2015-10-27 [60] => 2015-11-10 [61] => 2015-11-24 [62] => 2015-12-08 [63] => 2016-01-05 [64] => 2016-01-19 [65] => 2016-02-16 [66] => 2016-03-01 )

阵列B:

Array ( [0] => 2014-06-05 [1] => 2015-03-02 [2] => 2015-12-03 )

编辑2:

只是进一步的解释,所以我最终希望它输出如下内容:

[29日,44岁,62]

这是数组A中离B[0,1和2]最近的键

如果我理解你的问题,下面的代码将帮助你。我测试了它,如果一些数组,它工作得很好。

function getClosetsDates ($A, $B) 
{
  $answer = array();
  foreach ($B as $stringDateToAnalyze) {
    //create a DateTimeObject with your B date
    $dateToAnalyze = new DateTime($stringDateToAnalyze);
    //iterates over A and compare your date with each date in A
    $keyOfClosestDate = 0;//consider the first date of A as the closest one
    $previousDifference = 9999;
    foreach ($A as $key => $stringDateToCompare) {
      $dateToCompare = new DateTime($stringDateToCompare);
      //compare your B date with the current A date
      $difference = $dateToCompare->diff($dateToAnalyze)->days;
      if ($difference <= $previousDifference) {
        $previousDifference = $difference;
        $keyOfClosestDate = $key;
        //you can unset $A[$key] if its not used anymore
      }
    }
    $answer[] = $keyOfClosestDate;
  }
  return $answer;
}
下面是我测试的一个例子。我选择较小的数组来改进解决方案的分析。
$A = array
(
    '0'=> "2013-02-19",
    '1'=> "2013-02-22",
    '2'=> "2013-02-15",
    '3'=> "2013-01-29",
    '4'=> "2013-01-27"
);
$B = array
(
    '0'=> "2013-02-18",
    '1'=> "2013-02-12"
);
var_dump(getClosetsDates($A,$B));

结果是:

array(2) {
  [0] =>
  int(0)
  [1] =>
  int(2)
}

如果您需要做任何改进,我建议参考DateInterval和diff方法的文档