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

时间:2016-09-19 14:58:45

标签: php arrays date comparison

复杂的问题,但我会尽可能简单地解释它:

我有两个数组,A和B.

阵列A包含进行测量的日期。

数组B包含测量中发现错误的日期。

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

我想要做的是在图表上添加标记,在最接近的日期[Key Value]添加到数组B中的那些。

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

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

编辑:数组如下所示:

数组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]

这些是最接近B [0,1和2]的数组A键。

1 个答案:

答案 0 :(得分:0)

如果我理解您的问题,下面的代码会对您有所帮助。我测试了一些数组并且它工作正常。

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)
}

如果您需要进行任何改进,我建议使用DateIntervaldiff方法的文档。