0

I all, I've been staring at this for hours so any help is greatly appreciated. I have an array...

$aircraft = [
  'N7826C' => ['nnum' => 'N7826C', 'name' => 'ANAHEIM POLICE DEPT', 'icon' => 'police', 'lat' => '', 'lng' => '', 'status' => '0'],
  'N226PD' => ['nnum' => 'N226PD', 'name' => 'ANAHEIM POLICE DEPT', 'icon' => 'police', 'lat' => '', 'lng' => '', 'status' => '0'],
  'N326PD' => ['nnum' => 'N326PD', 'name' => 'CITY OF ANAHEIM', 'icon' => 'police', 'lat' => '', 'lng' => '', 'status' => '0'],
  'N826PD' => ['nnum' => 'N826PD', 'name' => 'CITY OF ANAHEIM', 'icon' => 'police', 'lat' => '', 'lng' => '', 'status' => '0']
];

With that array, I implode it to get a list of the nnums to pass through a API....

$aircraftNNUMlist = implode(',', array_map(function($v) { return $v['nnum']; }, $aircraft));

$json = file_get_contents('https://airlabs.co/api/v9/flights?_fields=reg_number,lat,lng&reg_number='.$aircraftNNUMlist.'&api_key=XXXXX');

That API returns...

{
  "request": {
    "lang": "en",
    "currency": "USD",
    "time": 15,
    "id": "c9by9lmq1q0",
    "server": "z",
    "host": "airlabs.co",
    "pid": 322387,
    "key": {
      "id": 19146,
      "api_key": "xxxxxxxxxxxxxxxx",
      "type": "free",
      "expired": "2022-08-20T22:00:00.000Z",
      "registered": "2022-07-19T03:51:04.000Z",
      "limits_by_hour": 2500,
      "limits_by_minute": 250,
      "limits_by_month": 1000,
      "limits_total": 628
    },
    "params": {
      "_fields": "reg_number,lat,lng",
      "reg_number": "N60NT,N40NT,N30NT,N10NT",
      "lang": "en"
    },
    "version": 9,
    "method": "flights",
    "client": {
      "ip": "xxxxxxxxxxxxxxxxx",
      "geo": {
        "country_code": "US",
        "country": "United States",
        "continent": "North America",
        "city": "Provo",
        "lat": 40.2181,
        "lng": -111.6133,
        "timezone": "America/Denver"
      },
      "connection": {
        "type": "corporate",
        "isp_code": 46606,
        "isp_name": "Unified Layer"
      },
      "device": {},
      "agent": {},
      "karma": {
        "is_blocked": false,
        "is_crawler": false,
        "is_bot": false,
        "is_friend": false,
        "is_regular": true
      }
    }
  },
  "response": [
    {
      "reg_number": "N60NT",
      "lat": 34.11,
      "lng": -117.69
    }
  ],
  "terms": "Unauthorized access is prohibited and punishable by law. \nReselling data 'As Is' without AirLabs.Co permission is strictly prohibited. \nFull terms on https://airlabs.co/. \nContact us info@airlabs.co"
}

I am having trouble looping through the "response" of the API return (stored at $json) to update the corresponding index in $aircraft. If a nnum isn't currently active there will not be a entry in "response" for instance "response" only has N60NT in the return array. I am fairly new to arrays so I've been taking swings in the dark and nothing seems to be right.

  • You can use `implode(',', array_column($aircraft, 'nnum'))` for your url; or even simpler would be to extract the keys: `implode(',', array_keys($aircraft))`. Inserting your found data into your original array _can_ be done via the answer from ITgoldman or via a body-less loop: `foreach ($array['response'] as ['reg_number' => $reg, 'lat' => $aircraft[$reg]['lat'], 'lng' => $aircraft[$reg]['lng']]);` or you could `unset('$row['reg_number']);` then use `array_replace()` to overwrite the lat and lng elements (inside your loop). – mickmackusa Jul 21 '22 at 23:00

1 Answers1

0

I presume you have json_decoded your api json response into a PHP array. Well actually:

$arr = json_decode($json_form_api, true);
$response = $arr["response"];
foreach ($response as $sub) {
    $reg = $sub["reg_number"];
    $aircraft[$reg]["lat"] = $sub["lat"];
    $aircraft[$reg]["lng"] = $sub["lng"];
}
IT goldman
  • 14,885
  • 2
  • 14
  • 28