0

I have the following as part of a JSON array

"appendix": {
  "airlines": [
   {
    "fs": "AA",
    "iata": "AA",
    "icao": "AAL",
    "name": "American Airlines",
    "phoneNumber": "08457-567-567",
    "active": true
   },
   {
    "fs": "LY",
    "iata": "LY",
    "icao": "ELY",
    "name": "El Al",
    "phoneNumber": "+ 972-3-9771111",
    "active": true
   }

And I want to get the only the "name" of in the string that has an "iata" of "AA" (i.e. "American Airlines"). I tried writing a for loop where

$iata = "AA"
$appendix_airlines = $array["airlines"][0];

$app_airlines_length = count($appendix_airlines);      

        for ($i = 0; $i <= $app_airlines_length; $i ++) {
                if ($appendix_airlines[$i]["iata"] == "AA") {
                    echo $appendix_airlines[$i]["name"];
                };
        }

However, this just returns an undefined offset = 0, undefined offset = 1 etc error. How do I fix this?

Essentially, I want it to go and get the name of the airline based on the IATA code that got inputted.

jimothy
  • 13
  • 2

3 Answers3

0

Here is the script with json_decode,

$temp = json_decode($json,true); // true as 2nd for raw array
$iata = "AA";
foreach ($temp['appendix']['airlines'] as $key => $value) {
    if($value['iata'] == $iata){
         echo $value["name"]."<br>";
    }
}

Demo

Output

American Airlines
Rahul
  • 18,271
  • 7
  • 41
  • 60
0

You are not selected array, you selected Array's first object:

$appendix_airlines = $array["airlines"][0];

Just change to:

$appendix_airlines = $array["airlines"];

$app_airlines_length = count($appendix_airlines);      

        for ($i = 0; $i < $app_airlines_length; $i ++) {
                if ($appendix_airlines[$i]["iata"] == "AA") {
                    echo $appendix_airlines[$i]["name"];
                };
        }

Note:ArrayIndexOutOfBondsException:

for ($i = 0; $i <= $app_airlines_length; $i ++)

Code should be $i < $app_airlines_length

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
0

array_column is great for wrangling JSON in Php:

<?php

$json =<<< 'JSON'
{
    "appendix": {
        "airlines": [
            {
                "fs": "AA",
                "iata": "AA",
                "icao": "AAL",
                "name": "American Airlines",
                "phoneNumber": "08457-567-567",
                "active": true
            },
            {
                "fs": "LY",
                "iata": "LY",
                "icao": "ELY",
                "name": "El Al",
                "phoneNumber": "+ 972-3-9771111",
                "active": true
            }
        ]
    }
}
JSON;
$data       = json_decode($json, true);
$iata_names = array_column(
    $data['appendix']['airlines'], 
    'name', 
    'iata'
);

echo $iata_names['AA'] ?? 'N/A';

Output:

American Airlines

The result of array_column is the following:

var_export($iata_names);

Output:

array (
    'AA' => 'American Airlines',
    'LY' => 'El Al',
  )
Progrock
  • 7,373
  • 1
  • 19
  • 25