如何使用Array php返回JSON对象

时间:2018-06-06 20:52:46

标签: php json guzzle

我有这个PHP代码从JSON文件返回数组,但是我无法返回对象(机场信息结构)以及到达等。

道歉,如果不是很清楚,我对此很新。

{
    "AirportBoardsResult": {
        "airport": "NZAP",
        "airport_info": {
            "airport_code": "NZAP",
            "name": "Taupo",
            "elevation": 1335.0,
            "city": "Taupo",
            "state": "",
            "longitude": 176.084444,
            "latitude": -38.739723,
            "timezone": ":Pacific/Auckland",
            "country_code": "NZ",
            "wiki_url": "https://en.wikipedia.org/wiki/Taupo_Airport",
            "alternate_ident": "TUO"
        },
        "arrivals": {
            "num_flights": 6,
            "next_offset": -1,
            "flights": [{
                "ident": "SDA806",
                "faFlightID": "SDA806-1528092600-schedule-0000",
                "airline": "SDA",
                "airline_iata": "S8",
                "flightnumber": "806",
                "tailnumber": "ZK-PLV",
                "type": "Form_Airline",
                "blocked": false,
                "diverted": false,
                "cancelled": false,
                "origin": {
                    "code": "NZWN",
                    "city": "Wellington",
                    "alternate_ident": "WLG",
                    "airport_name": "Wellington Int'l"
                },
                "destination": {
                    "code": "NZAP",
                    "city": "Taupo",
                    "alternate_ident": "TUO",
                    "airport_name": "Taupo"
                },
                "filed_ete": 3480,
                "route": "KADNU1Q KAPTI WNAP2",
                "filed_altitude": 210,
                "display_filed_altitude": "21,000 feet",
                "filed_airspeed_kts": 250,
                "distance_filed": 191,
                "filed_departure_time": {
                    "epoch": 1528265400,
                    "tz": "NZST",
                    "dow": "Wednesday",
                    "time": "06:10PM",
                    "date": "06/06/2018",
                    "localtime": 1528308600
                },
                "estimated_departure_time": {
                    "epoch": 1528265889,
                    "tz": "NZST",
                    "dow": "Wednesday",
                    "time": "06:18PM",
                    "date": "06/06/2018",
                    "localtime": 1528309089
                },
                "actual_departure_time": {
                    "epoch": 1528265889,
                    "tz": "NZST",
                    "dow": "Wednesday",
                    "time": "06:18PM",
                    "date": "06/06/2018",
                    "localtime": 1528309089
                },
                "departure_delay": 489,
                "filed_arrival_time": {
                    "epoch": 1528268880,
                    "tz": "NZST",
                    "dow": "Wednesday",
                    "time": "07:08PM",
                    "date": "06/06/2018",
                    "localtime": 1528312080
                },
                "estimated_arrival_time": {
                    "epoch": 1528269502,
                    "tz": "NZST",
                    "dow": "Wednesday",
                    "time": "07:18PM",
                    "date": "06/06/2018",
                    "localtime": 1528312702
                },
                "actual_arrival_time": {
                    "epoch": 1528269180,
                    "tz": "NZST",
                    "dow": "Wednesday",
                    "time": "07:13PM",
                    "date": "06/06/2018",
                    "localtime": 1528312380
                },
                "arrival_delay": 300,
                "status": "Arrived",
                "progress_percent": 100,
                "aircrafttype": "PC12",
                "full_aircrafttype": "L/PC12",
                "adhoc": false
            }]
        };
杰森:

Could not find any matches for io.fabric.tools:gradle:1.+ as no versions of io.fabric.tools:gradle are available.

1 个答案:

答案 0 :(得分:0)

在循环后将机场信息添加到结果中。

if ($fxml_response->getStatusCode() == 200 && !array_key_exists('error', $body)) {
    foreach (['arrivals', 'departures', 'enroute', 'scheduled',] as $board) {
        if($body['AirportBoardsResult'][$board]) {
            $boardFlights = $body['AirportBoardsResult'][$board]['flights'];
            $response[$board] = $boardFlights;
        }
    }
    $response['airport_info'] = $body['AirportBoardsResult']['airport_info'];
}