logo

dmpop

Aircraft photography, spaghetti code, half-baked thoughts

Guestbook Reads
Currently reading: Gli occhi di Venezia, Alessandro Barbero


ADS-B station: the code that even I can understand

A small but important (for me) programming victory. To get a list of hex codes of all aircraft detected by readsb, I used this piece of code suggested by phind:

$json = file_get_contents("/run/readsb/aircraft.json");
$array = json_decode($json, true);
$flights = array();
array_walk_recursive($array, function ($v, $k) use (&$flights) {
    if ($k === 'hex') {
        $flights[] = strtoupper($v);
    }
});

It worked, but I really couldn't wrap my head around it. It took me a while to come up with a solution that even I can understand, but I finally did it:

$json = file_get_contents("/run/readsb/aircraft.json");
$array = json_decode($json, true);
$flights = array();
for ($i = 0; $i < count($array['aircraft']); $i++) {
    array_push($flights, strtoupper($array['aircraft'][$i]['hex']));
};

Published: 2024-01-14Reads: 489
Comment by emailBack
dmpop

dmpop

Linux user, amateur photographer, aviation enthusiast, clueless developer of ADS-B station, Pellicola and Tinble, author of a bunch of books. I can mix a decent Negroni, too. You can reach me at me@dmpop.xyz