用PHP解码json多维数组

用PHP解码json多维数组,php,arrays,json,multidimensional-array,Php,Arrays,Json,Multidimensional Array,我从我的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" },

我从我的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"
    }
}
}
我想用 foreach在php中。这是我的密码:

foreach($json->data as$flight\u list)
{
foreach($flight\u list->offerItems->services->segments->FlightSegments as$flight)
{
回声“;
echo“出发地点:“..flight->出发->iataCode.”
; 回显“起飞时间:“.$flight->起飞->在”。
”; echo“Arival to:”.$flight->arrival->iataCode.“
”; echo“Arival time:”.$flight->arrival->at.“
”; 回声“; } }


但它不起作用。我很困惑。如何处理这个多维数组。

我假设您已经用
$JSON=JSON\u decode($response)解码了JSON文本。将结果称为“JSON”有点尴尬。您得到的响应是JSON,但一旦您解码它,它就是一个PHP对象(具有嵌套数组属性,同样具有对象属性,…等等);这不是JSON

现在来看看你的代码。外环很好:

foreach ($json->data as $flight_list)
但下一个循环是错误的。它忽略了一个事实,即
提供项
服务
都是(索引的)数组,因此您无法执行
提供项->某些操作。相反,您应该迭代这些数组:

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";
            }
        }
    }
}

json不是对象,不是数组,而是字符串(正如您所展示的)。那么,如何获得
$json
变量???希望通过
json\u解码(包含上述json字符串的变量)这不是一个“多维数组”,而是对象层次结构和数组的混合体。可能是重复的,或者为什么在第一个“链接”处而不是在其他链接处拆分链?我认为每个级别都需要单独循环。