mysql - Merge results of single JSON output with PHP -


i have json array per below:

{ "aadata": [     {         "date_time": "23",         "traffic": "22",         "direction": "sent"     },     {         "date_time": "24",         "traffic": "55",         "direction": "sent"     },     {         "date_time": "25",         "traffic": "60",         "direction": "sent"     },     {         "date_time": "26",         "traffic": "43",         "direction": "sent"     },     {         "date_time": "27",         "traffic": "50",         "direction": "sent"     },     {         "date_time": "23",         "traffic": "50",         "direction": "received"     },     {         "date_time": "24",         "traffic": "42",         "direction": "received"     },     {         "date_time": "25",         "traffic": "52",         "direction": "received"     },     {         "date_time": "26",         "traffic": "47",         "direction": "received"     },     {         "date_time": "27",         "traffic": "36",         "direction": "received"     } ] } 

what i'd combine results same date single entry - date_time 23 want appear this

    "date_time": "23",     "traffic-sent": "22",     "traffic-received": "50" 

i'd php if possible? data coming 2 separate mysql queries, coming different mysql databases. i've tried combining output of query need (tried joins , unions) can't past separation of results per first example.

the part of sql query creating json looks this:

while($row = mysqli_fetch_assoc($result)) {     $model[$i]['date_time'] = $row['the_day'];     $model[$i]['traffic']   = $row['traffic'];     $model[$i]['direction'] = $row['table_name'];     $i++; } 

and sql looks this:

(select day(`time`) the_day, count(accounts.accname) traffic, "sent" table_name bss.ss_sent left join bss.accounts on ss_sent.customer = accounts.accname yearweek(`time`) = yearweek(current_date) ,  customer != " " , accshortname = "qrr" group the_day)  union     (select day(date_time) the_day, count(as_task) traffic, "received" table_name im_stats.as_counter as_task = "qrr3 incoming" , yearweek(date_time) = yearweek(current_date) group the_day order the_day) 

if can advise of way combine results i'd appreciate it.

update:

this how i've entered populus's code:

$i = 0; while($row = mysqli_fetch_assoc($result)) {     $model[$i]['date_time'] = $row['the_day'];     $model[$i]['traffic']   = $row['traffic'];     $model[$i]['direction'] = $row['table_name'];     $i++; }  $combined = array();  foreach ($model $val) {     $date_time = $val['date_time'];     if (!isset($combined[$date_time)) {         $combined[$date_time] = array(             'date_time' => $date_time,             'traffic_sent' => 0,             'traffic_received' => 0,     ); } if ('received' == $val['direction']) {     $combined[$date_time]['traffic_received'] += $val['traffic']; } else {     $combined[$date_time]['traffic_sent'] += $val['traffic']; } }     header('content-type: application/json');  print json_encode(array('aadata' => $combined), json_pretty_print); 

this done using sql (which haven't provided), if want php:

$combined = array();  foreach ($model $val) {     $date_time = $val['date_time'];     if (!isset($combined[$date_time])) {         $combined[$date_time] = array(             'date_time' => $date_time,             'traffic_sent' => 0,             'traffic_received' => 0,         );     }     if ('received' == $val['direction']) {         $combined[$date_time]['traffic_received'] += $val['traffic'];     } else {         $combined[$date_time]['traffic_sent'] += $val['traffic'];     } } 

your desired array in $combined. if don't want keys, can remove it:

$result = array_values($combined); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -