query('select o.* from results as o inner join (select host, max(timestamp) as latest from results group by host) as i on o.host= i.host where o.timestamp = i.latest'); $out = []; foreach($result as $row) { $out[$row['host']] = ['time' => $row['timestamp'], 'status' => $row['returncode'], 'detail' => $row['detail']]; } print(json_encode($out)); ?>