php - Create zero values for missing items in foreach loop -


i have application extracts information mysql between 2 dates , returns associative array. producing graph information have dates missing dates in database have no information return. cannot fix on mysql side have read access database.

my database method retrieves associative array below:

[0] => array         (             [number_of_calls] => 151             [total_call_time] => 00:01:30             [average_call] => 00:02:00             [date(calldate)] => 2016-03-18             [direction] => outbound         ) 

what hoping create daterange form below:

    //create data range array     $begin = new datetime( $datefrom );     $end = new datetime( $dateto );     $end = $end->modify( '+1 day' );     $interval = new dateinterval('p1d');     $daterange = new dateperiod($begin, $interval ,$end); 

and use foreach loop iterate through selected dates of daterange , pull value associative array dates match , if not insert 0 values below:

        [number_of_calls] => 0         [total_call_time] => 00:00:00         [average_call] => 00:00:00 

i need final array end in date order. can me please?

you can transform $result array use date(calldate) keys.

$keys = []; foreach ($result $item) {     $keys[] = $item['date(calldate)']; } $result = array_combine($keys, $result); 

and array that:

[2016-03-18] => array     (         [number_of_calls] => 151         [total_call_time] => 00:01:30         [average_call] => 00:02:00         [date(calldate)] => 2016-03-18         [direction] => outbound     ) 

and can check if date presented simple command:

$key = $datevalue->format('y-m-d'); if (isset($result[$key])) {     // date exists, use } else {     // date not exists, create empty value } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -