解析PHP

时间:2017-02-23 13:47:51

标签: php json api parsing foreach

我正在创建旅行应用程序并尝试实施QPX express API。我已经取得了一些成功,但我正在寻求帮助。

我正在尝试解析到达和离开时间,该时间位于trips/tripOption/slice/segment/leg

我试图从以下代码解析:(抱歉这么长时间)

{
    "kind": "qpxExpress#tripsSearch", "trips": {

        "kind": "qpxexpress#tripOptions",
        "requestId": "zGRd2g7SO2OR3uDEu0Q3Rc",
        "data": {
            "kind": "qpxexpress#data",
            "airport": [
              {
                  "kind": "qpxexpress#airportData",
                  "code": "ABZ",
                  "city": "ABZ",
                  "name": "Aberdeen Dyce"
              },
              {
                  "kind": "qpxexpress#airportData",
                  "code": "LHR",
                  "city": "LON",
                  "name": "London Heathrow"
              }
            ],
            "city": [
              {
                  "kind": "qpxexpress#cityData",
                  "code": "ABZ",
                  "name": "Aberdeen"
              },
              {
                  "kind": "qpxexpress#cityData",
                  "code": "LON",
                  "name": "London"
              }
            ],
            "aircraft": [
              {
                  "kind": "qpxexpress#aircraftData",
                  "code": "319",
                  "name": "Airbus A319"
              },
              {
                  "kind": "qpxexpress#aircraftData",
                  "code": "320",
                  "name": "Airbus A320"
              },
              {
                  "kind": "qpxexpress#aircraftData",
                  "code": "321",
                  "name": "Airbus A321"
              }
            ],
            "tax": [
              {
                  "kind": "qpxexpress#taxData",
                  "id": "UB_001",
                  "name": "United Kingdom Passenger Service Charge Departures"
              },
              {
                  "kind": "qpxexpress#taxData",
                  "id": "GB_001",
                  "name": "United Kingdom Air Passenger Duty Apd"
              }
            ],
            "carrier": [
              {
                  "kind": "qpxexpress#carrierData",
                  "code": "BA",
                  "name": "British Airways"
              }
            ]
        },
        "tripOption": [
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR002",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 100,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 100,
                          "flight": {
                              "carrier": "BA",
                              "number": "1309"
                          },
                          "id": "G-8Do8J4Ed1CCSbw",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 2,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "LeISp98KIk6mBaM9",
                                "aircraft": "320",
                                "arrivalTime": "2017-02-25T16:15+00:00",
                                "departureTime": "2017-02-25T14:35+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 100,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "G-8Do8J4Ed1CCSbw"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T09:34-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR005",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 105,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 105,
                          "flight": {
                              "carrier": "BA",
                              "number": "1303"
                          },
                          "id": "G8zQDGLwjfHKE25K",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 2,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "LWDGrQuSyJsv4smo",
                                "aircraft": "319",
                                "arrivalTime": "2017-02-25T09:30+00:00",
                                "departureTime": "2017-02-25T07:45+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 105,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "G8zQDGLwjfHKE25K"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T02:44-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR004",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 100,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 100,
                          "flight": {
                              "carrier": "BA",
                              "number": "1311"
                          },
                          "id": "GrrTYvxj3OBwLUle",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 1,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "LLa2rlu3AEMaQJ-7",
                                "aircraft": "320",
                                "arrivalTime": "2017-02-25T14:00+00:00",
                                "departureTime": "2017-02-25T12:20+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 100,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "GrrTYvxj3OBwLUle"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T07:19-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR003",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 100,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 100,
                          "flight": {
                              "carrier": "BA",
                              "number": "1313"
                          },
                          "id": "G9i8bJ8e7SyQdDse",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 2,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "L+53r7MThymrOjmD",
                                "aircraft": "321",
                                "arrivalTime": "2017-02-25T18:50+00:00",
                                "departureTime": "2017-02-25T17:10+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 100,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "G9i8bJ8e7SyQdDse"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T12:09-05:00",
                    "ptc": "ADT"
                }
              ]
          },
          {
              "kind": "qpxexpress#tripOption",
              "saleTotal": "GBP185.93",
              "id": "GNz9mcDQwhANkULUdCtRhR001",
              "slice": [
                {
                    "kind": "qpxexpress#sliceInfo",
                    "duration": 95,
                    "segment": [
                      {
                          "kind": "qpxexpress#segmentInfo",
                          "duration": 95,
                          "flight": {
                              "carrier": "BA",
                              "number": "1307"
                          },
                          "id": "GQZ+5WcC5Xyv4NSo",
                          "cabin": "COACH",
                          "bookingCode": "H",
                          "bookingCodeCount": 1,
                          "marriedSegmentGroup": "0",
                          "leg": [
                            {
                                "kind": "qpxexpress#legInfo",
                                "id": "L4AarejVfUroJVY-",
                                "aircraft": "319",
                                "arrivalTime": "2017-02-25T12:10+00:00",
                                "departureTime": "2017-02-25T10:35+00:00",
                                "origin": "ABZ",
                                "destination": "LHR",
                                "destinationTerminal": "5",
                                "duration": 95,
                                "mileage": 401,
                                "meal": "Food and Beverages for Purchase"
                            }
                          ]
                      }
                    ]
                }
              ],
              "pricing": [
                {
                    "kind": "qpxexpress#pricingInfo",
                    "fare": [
                      {
                          "kind": "qpxexpress#fareInfo",
                          "id": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "carrier": "BA",
                          "origin": "ABZ",
                          "destination": "LON",
                          "basisCode": "HV2RO"
                      }
                    ],
                    "segmentPricing": [
                      {
                          "kind": "qpxexpress#segmentPricing",
                          "fareId": "Afuj959G6F0Y7FfQbhCAsEgIdVd1sVrY3kSByE7vWKTMM",
                          "segmentId": "GQZ+5WcC5Xyv4NSo"
                      }
                    ],
                    "baseFareTotal": "GBP161.00",
                    "saleFareTotal": "GBP161.00",
                    "saleTaxTotal": "GBP24.93",
                    "saleTotal": "GBP185.93",
                    "passengers": {
                        "kind": "qpxexpress#passengerCounts",
                        "adultCount": 1
                    },
                    "tax": [
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "UB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "UB",
                          "country": "GB",
                          "salePrice": "GBP11.93"
                      },
                      {
                          "kind": "qpxexpress#taxInfo",
                          "id": "GB_001",
                          "chargeType": "GOVERNMENT",
                          "code": "GB",
                          "country": "GB",
                          "salePrice": "GBP13.00"
                      }
                    ],
                    "fareCalculation": "ABZ BA LON 161.00HV2RO GBP 161.00 END XT 13.00GB 11.93UB",
                    "latestTicketingTime": "2017-02-25T05:34-05:00",
                    "ptc": "ADT"
                }
              ]
           }
        ]
    }
}

我有两个foreach循环工作来访问机场和价格:

foreach($json['trips']['data']['airport'] as $item) {
    echo "<div>". $item['name'] . "</div>";
}

foreach($json['trips']['tripOption'] as $item) {
    echo "<div>". $item['saleTotal'] . "</div>";
}

以下代码无法访问到达时间,我不确定原因!

foreach($json['trips']['tripOption']['slice']['segment']['leg'] as $item) {
    echo "<div>". $item['arrivalTime'] . "</div>";
}

我还使用下面的代码来解析我的JSON响应。

$json = json_decode($results, true);

1 个答案:

答案 0 :(得分:0)

哇,这是一个安静的数据集,让你的头脑。

这将打印到达时间

foreach($json['trips']['tripOption'] as $options) {
    foreach ($options['slice'] as $slices){
        foreach ($slices['segment'] as $segments){
            foreach ($segments['leg'] as $leg ) {
                echo "<div>". $leg['arrivalTime'] . "</div>\n";
            }
        }
    }
}

结果

<div>2017-02-25T16:15+00:00</div>
<div>2017-02-25T09:30+00:00</div>
<div>2017-02-25T14:00+00:00</div>
<div>2017-02-25T18:50+00:00</div>
<div>2017-02-25T12:10+00:00</div>