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.