php - Match two arrays based on dates -
php - Match two arrays based on dates -
i seek match arrays based on dates. first array generated function (getdaterange), sec array comes wordpress database.
function getdaterange($startdate, $enddate, $format="y-m-d") { //create output variable $datesarray = array(); //calculate number of days in range $total_days = round(abs(strtotime($enddate) - strtotime($startdate)) / 86400, 0) + 1; if($total_days<0) { homecoming false; } //populate array of weekdays , counts for($day=0; $day<$total_days; $day++) { $datesarray[] = date($format, strtotime("{$startdate} + {$day} days")); } //return results array homecoming $datesarray; } $sql = "select date(datetime) date, sum(amount) amount sales grouping 1"; $results = $wpdb->get_results($sql, array_a); // generate date range $daterange = getdaterange('2014-10-01', '2014-10-06'); foreach($daterange $date) { echo $date . ' | '; if (array_key_exists($date, $results)) { echo 'ok'; } else { echo '0'; } echo '<br />'; }
with code above don't matching values:
2014-10-01 | 0 2014-10-02 | 0 2014-10-03 | 0 2014-10-04 | 0 2014-10-05 | 0 2014-10-06 | 0
the desired result is:
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 - result output numerically indexed array of associative arrays, using column names keys
from codex
so array_key_exists
search date in keys of $results
array, has numeric keys.
you implement search function multidimensional arrays instead of array_key_exists
function searchfordate($date, $array) { foreach ($array $key => $val) { if ($val['date'] === $date) { homecoming $key; } } homecoming null; }
modified from: php multi dimensional array search
php arrays wordpress
Comments
Post a Comment