1 回答

TA貢獻1802條經(jīng)驗 獲得超6個贊
無需嘗試使查詢復(fù)雜化,您只需稍微更改應(yīng)用程序 (PHP) 代碼即可。獲取數(shù)據(jù)庫中可用日期的查詢結(jié)果。現(xiàn)在,在 PHP 代碼中,只需檢查該日期的計數(shù)是否可用。如果是,使用計數(shù),否則設(shè)置為零。
// Change the SQL query result to an array with date as key, and count as value
$reacts_mod = array_combine(array_column($reacts, 'reactedDate'),
array_column($reacts, 'count'));
// Now prepare the $result
$result = array();
// Loop over input dates here
foreach ($dates as $dt) {
// If you have count obtained from the query result, then consider that else 0
if ( isset($reacts_mod[$dt]) ) {
$result[] = $reacts_mod[$dt];
} else {
$result[] = 0;
}
// Note: PHP7 code would be simply:
// $result[] = $reacts_mod[$dt] ?? 0;
}
- 1 回答
- 0 關(guān)注
- 532 瀏覽
添加回答
舉報