1

I got a response from my web service which is a multidimensional array. I want to decode this multidimensional array using JSON:

{
"meta": {
    "links": {
    "self": "http://test.api.amadeus.com/v1/shopping/flight-offers?origin=NYC&destination=MAD&departureDate=2019-08-01&adults=1&nonStop=false&max=2"
    },
    "currency": "EUR",
    "defaults": {
        "nonStop": false,
        "adults": 1
    }
},
"data": [
    {
        "type": "flight-offer",
        "id": "1539956390004--540268760",
        "offerItems": [
            {
                "services": [
                    {
                        "segments": [
                            {
                                "flightSegment": {
                                    "departure": {
                                        "iataCode": "EWR",
                                        "terminal": "B",
                                        "at": "2019-08-01T17:45:00-04:00"
                                    },
                                    "arrival": {
                                        "iataCode": "LIS",
                                        "terminal": "1",
                                        "at": "2019-08-02T05:35:00+01:00"
                                    },
                                    "carrierCode": "TP",
                                    "number": "202",
                                    "aircraft": {
                                        "code": "332"
                                    },
                                    "operating": {
                                        "carrierCode": "TP",
                                        "number": "202"
                                    },
                                    "duration": "0DT6H50M"
                                },
                                "pricingDetailPerAdult": {
                                    "travelClass": "ECONOMY",
                                    "fareClass": "U",
                                    "availability": 1,
                                    "fareBasis": "UUSDSI0E"
                                }
                            },
                            {
                                "flightSegment": {
                                    "departure": {
                                        "iataCode": "LIS",
                                        "terminal": "1",
                                        "at": "2019-08-02T06:55:00+01:00"
                                    },
                                    "arrival": {
                                        "iataCode": "MAD",
                                        "terminal": "2",
                                        "at": "2019-08-02T09:10:00+02:00"
                                    },
                                    "carrierCode": "TP",
                                    "number": "1026",
                                    "aircraft": {
                                        "code": "319"
                                    },
                                    "operating": {
                                        "carrierCode": "TP",
                                        "number": "1026"
                                    },
                                    "duration": "0DT1H15M"
                                },
                                "pricingDetailPerAdult": {
                                    "travelClass": "ECONOMY",
                                    "fareClass": "U",
                                    "availability": 5,
                                    "fareBasis": "UUSDSI0E"
                                }
                            }
                        ]
                    }
                ],
                "price": {
                    "total": "259.91",
                    "totalTaxes": "185.91"
                },
                "pricePerAdult": {
                    "total": "259.91",
                    "totalTaxes": "185.91"
                }
            }
        ]
    },
    {
        "type": "flight-offer",
        "id": "1539956390004-765796655",
        "offerItems": [
            {
                "services": [
                    {
                    "segments": [
                        {
                        "flightSegment": {
                            "departure": {
                            "iataCode": "JFK",
                            "at": "2019-08-01T22:05:00-04:00"
                            },
                            "arrival": {
                            "iataCode": "MAD",
                            "at": "2019-08-02T11:30:00+02:00",
                            "terminal": "1"
                            },
                            "carrierCode": "UX",
                            "number": "92",
                            "aircraft": {
                            "code": "332"
                            },
                            "operating": {
                            "carrierCode": "UX",
                            "number": "92"
                            },
                            "duration": "0DT7H25M"
                        },
                        "pricingDetailPerAdult": {
                            "travelClass": "ECONOMY",
                            "fareClass": "M",
                            "availability": 9,
                            "fareBasis": "MYYOAE"
                        }
                        }
                    ]
                    }
                ],
                "price": {
                    "total": "1670.89",
                    "totalTaxes": "162.89"
                },
                "pricePerAdult": {
                    "total": "1670.89",
                    "totalTaxes": "162.89"
                }
            }
        ]
    }
],
"dictionaries": {
    "locations": {
        "JFK": {
            "subType": "AIRPORT",
            "detailedName": "JOHN F KENNEDY INTL"
        },
        "EWR": {
            "subType": "AIRPORT",
            "detailedName": "NEWARK LIBERTY INTL"
        },
        "MAD": {
            "subType": "AIRPORT",
            "detailedName": "ADOLFO SUAREZ BARAJAS"
        },
        "LIS": {
            "subType": "AIRPORT",
            "detailedName": "AIRPORT"
        }
    },
    "carriers": {
        "UX": "AIR EUROPA",
        "TP": "TAP PORTUGAL"
    },
    "currencies": {
        "EUR": "EURO"
    },
    "aircraft": {
        "319": "AIRBUS INDUSTRIE A319",
        "332": "AIRBUS INDUSTRIE A330-200"
    }
}
}

I want to list flights using foreach in php. Here is my code:

foreach ($json->data as $flight_list)
    {
    foreach ($flight_list->offerItems->services->segments->flightSegment as $flight )
    {
    echo "<div style='margin:3px'>";
    echo "Departure from:". $flight->departure->iataCode .'<br/>';
            echo "departure time:". $flight->departure->at .'<br/>';
            echo "Arival to:". $flight->arrival->iataCode .'<br/>';
            echo "Arival time:". $flight->arrival->at .'<br/>';
            echo "</div>";
        }
    }

but it does not work. I am confused. how can I deal with this multidimensional array.

MB_18
  • 1,620
  • 23
  • 37
  • json is not an object, not an array, it is a string (as you are showing it) . So, how did you get the `$json` variable ??? hopefully with `json_decode($theVariableThatContainsTheAboveJsonString);` – YvesLeBorg Mar 02 '19 at 17:24
  • This is _not_ a "multi dimensional array" but a mixture of object hierarchies and arrays. – arkascha Mar 02 '19 at 17:26
  • 2
    Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php), or https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php – YvesLeBorg Mar 02 '19 at 17:26
  • Why did you split your chain at the first "link" and not at the others ? I think you need to loop for each level individually. – Tuckbros Mar 02 '19 at 18:07

1 Answers1

3

I assume you have decoded the JSON text with $json = json_decode($response);. It is a bit awkward to call the result of that "JSON". It is the response you got which is JSON, but once you decode it, it is a PHP object (with nested array properties, which again have object properties, ...etc); that's not JSON.

Now to your code. The outer loop is fine:

foreach ($json->data as $flight_list)

But the next loop is wrong. It ignores the fact that offerItems, services, segments are all (indexed) arrays, so you cannot do offerItems->something. Instead, you should iterate those arrays:

foreach ($json->data as $flight_list) {
    foreach ($flight_list->offerItems as $offerItem) {
        foreach($offerItem->services as $service) {
            foreach($service->segments as $segment) {
                // Uncomment next line to see what you have at this level:
                //echo json_encode($segment) . "\n";

                // Example of what you could get and output:
                echo "from {$segment->flightSegment->departure->iataCode} to {$segment->flightSegment->arrival->iataCode}\n";
            }
        }
    }
}
trincot
  • 317,000
  • 35
  • 244
  • 286