2016-09-19 64 views
0

情结看问题,但我会尽量简单地解释它尽可能:比较两组阵列的日期,并找到匹配的日期最接近的键值在PHP

我有两个数组,A和B 。

数组A包含进行测量的日期。

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

我目前在图形(jpgraph)上有数组A作为X轴,为了确定在各个日期的测量结果发生了什么,将点作为标准差作为关键值。

我想什么做的就是添加标记的图,在最接近的日期[键值]那些在阵列B

从本质上讲,我想找到在阵列中的最接近的日期各输入数组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]

这些是最接近阵列A键到B [0,1和2]。

+0

你有数组的副本,以帮助将这个问题看得更好一点吗? –

+0

我已将它们添加到最初的帖子,谢谢! – TrailerTruck

回答

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

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

+0

未定义的变量:$ stringDateToCompare! – TrailerTruck

+0

@TrailerTruck。这是第一个foreach中的错字。它现在正确。 – James

+0

太好了,谢谢。现在解决了! – TrailerTruck