logo

dmpop

Aircraft photography, spaghetti code, half-baked thoughts

Reads Guestbook
Currently reading: Damascus Station, David McCloskey


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: 89
Back
Me

Me

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