使用PHP引用JSON格式的附录



我目前正在开发针对FlightStats时间表API,这是一个用户能够在给定的路线上搜索航班时间表,在给定的日期。

终点看起来像这样

https://api.flightstats.com/flex/schedules/rest/v1/json/from/{departing_airport}/to/{arriving_airport}/departing/{YYYY/MM/DD}?appId=APP_ID&appKey=API_KEY&codeType=iata&extendedOptions=includeDirects

用户将指定出发机场,到达机场和日期为年月日

为了给出一个示例响应,我将分别使用MEL, NRT和2015/11/12

{
 "request": {
  "departureAirport": {
   "requestedCode": "mel",
   "fsCode": "MEL"
  },
  "arrivalAirport": {
   "requestedCode": "nrt",
   "fsCode": "NRT"
  },
  "codeType": {
   "requested": "iata",
   "interpreted": "IATA"
  },
  "departing": true,
  "date": {
   "year": "2015",
   "month": "11",
   "day": "12",
   "interpreted": "2015-11-12"
  },
  "url": "https://api.flightstats.com/flex/schedules/rest/v1/json/from/mel/to/nrt/departing/2015/11/12"
 },
 "scheduledFlights": [
  {
   "carrierFsCode": "JQ",
   "flightNumber": "23",
   "departureAirportFsCode": "MEL",
   "arrivalAirportFsCode": "NRT",
   "stops": 0,
   "departureTerminal": "2",
   "arrivalTerminal": "3",
   "departureTime": "2015-11-12T00:55:00.000",
   "arrivalTime": "2015-11-12T08:45:00.000",
   "flightEquipmentIataCode": "787",
   "isCodeshare": false,
   "isWetlease": false,
   "serviceType": "J",
   "serviceClasses": [
    "R",
    "F",
    "J",
    "Y"
   ],
   "trafficRestrictions": [],
   "codeshares": [
    {
     "carrierFsCode": "JL",
     "flightNumber": "5086",
     "serviceType": "J",
     "serviceClasses": [
      "Y"
     ],
     "trafficRestrictions": [],
     "referenceCode": 2088960
    }
   ],
   "referenceCode": "1492-2106903--"
  },
  {
   "carrierFsCode": "JL",
   "flightNumber": "5086",
   "departureAirportFsCode": "MEL",
   "arrivalAirportFsCode": "NRT",
   "stops": 0,
   "departureTerminal": "2",
   "arrivalTerminal": "3",
   "departureTime": "2015-11-12T00:55:00.000",
   "arrivalTime": "2015-11-12T08:45:00.000",
   "flightEquipmentIataCode": "787",
   "isCodeshare": true,
   "isWetlease": false,
   "serviceType": "J",
   "serviceClasses": [
    "Y"
   ],
   "trafficRestrictions": [],
   "operator": {
    "carrierFsCode": "JQ",
    "flightNumber": "23",
    "serviceType": "J",
    "serviceClasses": [
     "R",
     "F",
     "J",
     "Y"
    ],
    "trafficRestrictions": []
   },
   "codeshares": [],
   "referenceCode": "1492-2106903--2088960"
  }
 ],
 "appendix": {
  "airlines": [
   {
    "fs": "JL",
    "iata": "JL",
    "icao": "JAL",
    "name": "JAL",
    "active": true
   },
   {
    "fs": "JQ",
    "iata": "JQ",
    "icao": "JST",
    "name": "Jetstar",
    "active": true
   }
  ],
  "airports": [
   {
    "fs": "NRT",
    "iata": "NRT",
    "icao": "RJAA",
    "name": "Narita International Airport",
    "street1": "成田空港第2PTB(バス), Narita",
    "street2": "Chiba Prefecture",
    "city": "Tokyo",
    "cityCode": "TYO",
    "countryCode": "JP",
    "countryName": "Japan",
    "regionName": "Asia",
    "timeZoneRegionName": "Asia/Tokyo",
    "localTime": "2015-09-21T09:37:31.611",
    "utcOffsetHours": 9,
    "latitude": 35.773213,
    "longitude": 140.387443,
    "elevationFeet": 135,
    "classification": 1,
    "active": true
   },
   {
    "fs": "MEL",
    "iata": "MEL",
    "icao": "YMML",
    "name": "Tullamarine Airport",
    "city": "Melbourne",
    "cityCode": "MEL",
    "stateCode": "VIC",
    "countryCode": "AU",
    "countryName": "Australia",
    "regionName": "Oceania",
    "timeZoneRegionName": "Australia/Sydney",
    "localTime": "2015-09-21T10:37:31.611",
    "utcOffsetHours": 10,
    "latitude": -37.669611,
    "longitude": 144.849777,
    "elevationFeet": 434,
    "classification": 1,
    "active": true
   }
  ],
  "equipments": [
   {
    "iata": "787",
    "name": "Boeing 787",
    "turboProp": false,
    "jet": true,
    "widebody": true,
    "regional": false
   }
  ]
 }
}

在这个阶段我想要的输出是[附录-> airline -> name] - [scheduledFlights -> carrierFsCode][scheduledFlights -> flightNumber]

捷星- JQ23

JAL - JL5086

因此,简而言之,为了获得所需的输出,我需要根据附录中的航空公司名称引用carrierFsCode。请记住,这个例子(为简洁起见)是一条低流量路线。以肯尼迪机场到希思罗机场为例,由于这条航线上的航班数量多,所以要复杂得多。

我的错误PHP现在是这样的

$flight_json=get_flight_stats($from,$to,$date);
$flight_stats=$flight_json['scheduledFlights'];
$airlines=$flight_json['appendix']['airlines'];
foreach($flight_stats as $flight_stat)
{
    echo $flight_stat['carrierFsCode'].$flight_stat['flightNumber']."<br>";
    echo "<br>";
}
foreach($airlines as $airline)
{
    echo $airline['name']."<br>";
}

返回

JQ23

JL5086

捷星

日航

get_flight_stats函数用于创建端点URL, get_file_contents, json_decode然后返回array_map。

如果有人问这个问题,我很抱歉,只是没能找到正确的搜索词,我发现自己有点卡住了。

(编辑)感谢@jolyonruss,我的最终代码如下:

foreach($flight_stats as $flight_stat)
{
    /* store the fsCode for this flight */
    $fsCode = $flight_stat['carrierFsCode'];
    foreach($airlines as $airline)
    {
        /* test to see if the airline's fsCode matches the flight */
        if($airline['fs'] === $fsCode)
        {
            echo $airline['name']." - ".$fsCode.$flight_stat['flightNumber']."<br>";
        }
    }
}

效果不错

看起来你需要嵌套你的for循环,像这样:

$flight_json=get_flight_stats($from,$to,$date);
$flight_stats=$flight_json['scheduledFlights'];
$airlines=$flight_json['appendix']['airlines'];
foreach($flight_stats as $flight_stat)
{
    echo $flight_stat['carrierFsCode'].$flight_stat['flightNumber']."<br>";
    echo "<br>";
    /* store the fsCode for this flight */
    $fsCode = $flight_stat['carrierFsCode'];
    foreach($airlines as $airline)
    {
        /* test to see if the airline's fsCode matches the flight */
        if($airline['fs'] === $fsCode) 
        {
            echo $airline['name']."<br>";
        }
    }
}

这是伪代码,但希望你能明白。

相关内容

  • 没有找到相关文章

最新更新