Created
September 19, 2018 19:02
-
-
Save mtvbrianking/359f53cea04a7c9aad9dc95149cc4ac5 to your computer and use it in GitHub Desktop.
Group exception events by device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$reports = array( | |
['device_id'=>'D523C45F', 'vehicle_reg_no'=>'UAB 158G', 'event_id'=>'41', 'event'=>'panic_emergency', 'reported_at'=>'2018-09-19 14:10:58', ], | |
['device_id'=>'D523C45F', 'vehicle_reg_no'=>'UAB 158G', 'event_id'=>'41', 'event'=>'panic_emergency', 'reported_at'=>'2018-09-19 14:10:50', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-19 13:34:58', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-19 13:24:58', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-19 13:14:57', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-14 20:19:03', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'56', 'event'=>'ext_power_disconnected', 'reported_at'=>'2018-09-14 20:11:45', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-13 13:40:30', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-13 13:30:30', ], | |
['device_id'=>'2F3C4790', 'vehicle_reg_no'=>'KCG 785H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-13 00:01:41', ], | |
['device_id'=>'C61233EF', 'vehicle_reg_no'=>'TAZ 849H', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-18 15:37:46', ], | |
['device_id'=>'1EC23C62', 'vehicle_reg_no'=>'RAW 856S', 'event_id'=>'305', 'event'=>'turning_over_speeding', 'reported_at'=>'2018-09-19 16:48:57', ], | |
['device_id'=>'1EC23C62', 'vehicle_reg_no'=>'RAW 856S', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-19 14:52:37', ], | |
['device_id'=>'1EC23C62', 'vehicle_reg_no'=>'RAW 856S', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-19 14:37:37', ], | |
['device_id'=>'1EC23C62', 'vehicle_reg_no'=>'RAW 856S', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-19 14:22:36', ], | |
['device_id'=>'4523C6A0', 'vehicle_reg_no'=>'SSD 451A', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-14 16:35:29', ], | |
['device_id'=>'4523C6A0', 'vehicle_reg_no'=>'SSD 451A', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-14 16:34:29', ], | |
['device_id'=>'4523C6A0', 'vehicle_reg_no'=>'SSD 451A', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-14 16:33:29', ], | |
['device_id'=>'4523C6A0', 'vehicle_reg_no'=>'SSD 451A', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-14 16:32:29', ], | |
['device_id'=>'4523C6A0', 'vehicle_reg_no'=>'SSD 451A', 'event_id'=>'276', 'event'=>'excessive_idle', 'reported_at'=>'2018-09-14 16:31:29', ], | |
); | |
$devices = []; | |
// print json_encode(['reports' => $reports]); | |
// exit(); | |
foreach($reports as $report) { | |
$device_idx = array_search($report['device_id'], array_column($devices, 'id')); | |
if(is_numeric($device_idx)) { | |
$event = array( | |
'id' => $report['event_id'], | |
'name' => $report['event'], | |
'reported_at' => $report['reported_at'], | |
); | |
$devices[$device_idx]['events'][] = $event; | |
continue; | |
} | |
$device = array( | |
'id' => $report['device_id'], | |
'vehicle' => $report['vehicle_reg_no'], | |
); | |
$event = array( | |
'id' => $report['event_id'], | |
'name' => $report['event'], | |
'reported_at' => $report['reported_at'], | |
); | |
$device['events'][] = $event; | |
$devices[] = $device; | |
} | |
print json_encode(['devices' => $devices]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment