Php 根据日期匹配两个数组

Php 根据日期匹配两个数组,php,arrays,wordpress,Php,Arrays,Wordpress,我尝试根据日期匹配数组。第一个数组由函数(getDateRange)生成,第二个数组来自我的Wordpress数据库 function getDateRange($startDate, $endDate, $format="Y-m-d") { //Create output variable $datesArray = array(); //Calculate number of days in the range $tot

我尝试根据日期匹配数组。第一个数组由函数(getDateRange)生成,第二个数组来自我的Wordpress数据库

function getDateRange($startDate, $endDate, $format="Y-m-d")
    {
        //Create output variable
        $datesArray = array();
        //Calculate number of days in the range
        $total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1;
        if($total_days<0) { return false; }
        //Populate array of weekdays and counts
        for($day=0; $day<$total_days; $day++)
        {
            $datesArray[] = date($format, strtotime("{$startDate} + {$day} days"));
        }
        //Return results array
        return $datesArray;
    }


$sql = "SELECT date(datetime) AS date, SUM(amount) as amount FROM sales GROUP BY 1";
$results = $wpdb->get_results($sql, ARRAY_A);

// Generate the date range 
$dateRange = getDateRange('2014-10-01', '2014-10-06');

foreach($dateRange as $date) {
 echo $date . ' | ';

    if (array_key_exists($date, $results)) {
    echo 'OK';

    } else {
     echo '0';   
    }
    echo '<br />';
}
预期的结果是:

2014-10-01 | 0
2014-10-02 | 0
2014-10-03 | OK
2014-10-04 | 0
2014-10-05 | OK
2014-10-06 | 0
ARRAY_A-结果将作为关联数组的数字索引数组输出,使用列名作为键

因此
array\u key\u exists
将在
$results
数组的键中搜索日期,该数组只有数字键

您可以实现多维数组的搜索功能,而不是
array\u key\u exists

function searchForDate($date, $array) {
    foreach ($array as $key => $val) {
        if ($val['date'] === $date) {
            return $key;
        }
    }
    return null;
}

修改自:

首先,正确的答案取决于
$results
数组的外观

 Array ( [0] => 2014-10-01 [1] => 2014-10-02 [2] => 2014-10-03 [3] => 2014-10-04 [4] => 2014-10-05 [5] => 2014-10-06 )
这就是您的
$dateRange
数组。通常,在foreach时,您将以一个简单字符串的形式获取每个日期,因此,反过来,您的
$date
var将是:
2014-10-01
2014-10-02
2014-10-03
2014-10-04
2014-10-05
2014-10-06

这些是在
if(array\u key\u exists($date,$results)){}
中使用的值。这意味着,使用此场景,您的
$results
数组必须具有日期作为键,因此它应该看起来像:
Array([2014-10-03]=>foo'[2014-10-06]=>bar')

在这之后,你肯定会有点击率。 但是,如果没有
$results
的转储,我不能保证这就是您所需要的

希望有帮助!
继续编码!
战神。

什么是
var\u dump($results)
返回的?
 Array ( [0] => 2014-10-01 [1] => 2014-10-02 [2] => 2014-10-03 [3] => 2014-10-04 [4] => 2014-10-05 [5] => 2014-10-06 )