-1

I am using PHP and curl to extract data from a flight travel API I am trying to loop the following JSON output from a travel API and trying to populate "flight_date" and "airport" into a variable using a loop.

{
    "pagination": {
        "limit": 1,
        "offset": 0
    },
    "data": [
        {
            "flight_date": "2021-12-19",
            "flight_status": "scheduled",
            "departure": {
                "airport": "Suvarnabhumi International",
                "timezone": "Asia/Bangkok"
                
            },
            "arrival": {
                "airport": "Seoul (Incheon)",
                "timezone": "Asia/Seoul",
                "iata": "ICN"
            },
            "airline": {
                "name": "Delta Air Lines"
            },
            "flight": {
                "number": "7918",
                "iata": "DL7918"
                "codeshared": {
                    "airline_name": "korean air",
                    "airline_iata": "ke"
                }
            },
            "aircraft": null,
            "live": null
        }
    ]
}

I keep getting array to getting Array to String conversion when I loop the above

The Mac
  • 93
  • 8

1 Answers1

0

What you actually need to do in order to use JSON as a PHP array is to use json_decode().

You also need to make sure that your JSON isn't malformed first though. I tried to debug the provided JSON like this:

$json[] = '{
  "pagination": {
      "limit": 2,
      "offset": 0,
      "count": 2,
      "total": 315303
  },
  "data": [
      {
          "flight_date": "2021-12-19",
          "flight_status": "scheduled",
          "departure": {
              "airport": "Suvarnabhumi International",
              "timezone": "Asia/Bangkok",
              
          },
          "arrival": {
              "airport": "Seoul (Incheon)",
              "timezone": "Asia/Seoul",
              
          },
          "airline": {
              "name": "Delta Air Lines",
              "iata": "DL",
              "icao": "DAL"
          },
          "flight": {
              "number": "7918",
              "iata": "DL7918",
              
              }
          },
          "aircraft": null,
          "live": null
      },
      {
          "flight_date": "2021-12-19",
          "flight_status": "scheduled",
          "departure": {
              "airport": "Suvarnabhumi International",
              "timezone": "Asia/Bangkok",
              
          },
          "arrival": {
              "airport": "Heathrow",
              "timezone": "Europe/London",

          },
          "airline": {
              "name": "Air Canada",
              "iata": "AC",
              "icao": "ACA"
          },
          "flight": {
              "number": "6123",
              "iata": "AC6123",
              }
          },
          "aircraft": null,
          "live": null
      }
  ]
}';

foreach ($json as $string) {
  echo 'Decoding: ' . $string;
  json_decode($string);

  switch (json_last_error()) {
      case JSON_ERROR_NONE:
          echo ' - No errors';
      break;
      case JSON_ERROR_DEPTH:
          echo ' - Maximum stack depth exceeded';
      break;
      case JSON_ERROR_STATE_MISMATCH:
          echo ' - Underflow or the modes mismatch';
      break;
      case JSON_ERROR_CTRL_CHAR:
          echo ' - Unexpected control character found';
      break;
      case JSON_ERROR_SYNTAX:
          echo ' - Syntax error, malformed JSON';
      break;
      case JSON_ERROR_UTF8:
          echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
      break;
      default:
          echo ' - Unknown error';
      break;
  }

  echo PHP_EOL;
}

and it appears that your JSON is malformed. You may use this site: https://jsonlint.com/ to check where is the issue with your json.

Nick Gr
  • 105
  • 10