我嘗試根據日期匹配數組。第一個數組由函數(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 | 0
2014-10-04 | 0
2014-10-05 | 0
2014-10-06 | 0
期望的結果是:
2014-10-01 | 0
2014-10-02 | 0
2014-10-03 | OK
2014-10-04 | 0
2014-10-05 | OK
2014-10-06 | 0
var_dump($ results)'返回什麼? – rnevius 2014-10-22 08:45:45