首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用数组php返回JSON对象

如何用数组php返回JSON对象
EN

Stack Overflow用户
提问于 2018-06-07 04:52:46
回答 1查看 42关注 0票数 0

我使用这段php代码从JSON文件中返回数组,无论何时我都不能返回对象(机场信息结构)以及到达等。

抱歉,如果这一点不是很清楚,我对此相当陌生。

代码语言:javascript
复制
Code: 
$fxml_response = $client->request('GET', 'AirportBoards', ['query' => $queryParams]);
try {
    $body = json_decode($fxml_response->getBody(), true);
    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;
            }
        }
    } else {
        $response['error'] = $body['error'];
    }
} catch (Exception $e) {
    echo json_encode(['error' => 'Failed to retrieve Airport Board details.']);
}
// Send back the data
header('Content-Type: application/json');
echo json_encode($response);

Json:

代码语言:javascript
复制
{
    "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
            }]
        };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 05:09:30

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

代码语言:javascript
复制
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'];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50729423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档