我从Web服务收到了一个多维数组响应。我想使用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"
}
}
}
我想使用以下方式列出航班 PHP中的foreach。这是我的代码:
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>";
}
}
但不起作用。我很困惑。我该如何处理这个多维数组。
答案 0 :(得分:3)
我假设您已经用$json = json_decode($response);
解码了JSON文本。调用该“ JSON” 的结果有点尴尬。它是JSON的 response ,但是一旦解码,它就是一个PHP对象(具有嵌套数组属性,又具有对象属性,等等);那不是JSON。
现在输入您的代码。外部循环很好:
foreach ($json->data as $flight_list)
但是下一个循环是错误的。它忽略了offerItems
,services
,segments
都是(索引)数组的事实,因此您不能执行offerItems->something
。相反,您应该迭代这些数组:
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";
}
}
}
}