获取嵌套的JSON值[PHP]

时间:2018-09-09 04:53:44

标签: php json foreach

我试图从嵌套的JSON中获取一些值,但是由于某些原因,我没有获取键的值。这是我的JSON

$data =
    {  
       "currency":"USD",
       "results":[  
          {  
             "itineraries":[  
                {  
                   "outbound":{  
                      "duration":"07:10",
                      "flights":[  
                         {  
                            "departs_at":"2018-09-25T22:50",
                            "arrives_at":"2018-09-26T11:00",
                            "origin":{  
                               "airport":"EWR"
                            },
                            "destination":{  
                               "airport":"STN"
                            },
                            "marketing_airline":"H1",
                            "operating_airline":"PF",
                            "flight_number":"1930",
                            "aircraft":"32S",
                            "booking_info":{  
                               "travel_class":"ECONOMY",
                               "booking_code":"T",
                               "seats_remaining":9
                            }
                         }
                      ]
                   },
                   "inbound":{  
                      "duration":"08:25",
                      "flights":[  
                         {  
                            "departs_at":"2018-09-27T17:55",
                            "arrives_at":"2018-09-27T21:20",
                            "origin":{  
                               "airport":"STN"
                            },
                            "destination":{  
                               "airport":"EWR"
                            },
                            "marketing_airline":"H1",
                            "operating_airline":"PF",
                            "flight_number":"1929",
                            "aircraft":"32S",
                            "booking_info":{  
                               "travel_class":"ECONOMY",
                               "booking_code":"T",
                               "seats_remaining":9
                            }
                         }
                      ]
                   }
                }
             ],
             "fare":{  
                "total_price":"538.39",
                "price_per_adult":{  
                   "total_fare":"538.39",
                   "tax":"237.39"
                },
                "restrictions":{  
                   "refundable":false,
                   "change_penalties":true
                }
             }
          },
          {  
             "itineraries":[  
                {  
                   "outbound":{  
                      "duration":"10:30",
                      "flights":[  
                         {  
                            "departs_at":"2018-09-25T18:55",
                            "arrives_at":"2018-09-26T04:55",
                            "origin":{  
                               "airport":"EWR",
                               "terminal":"B"
                            },
                            "destination":{  
                               "airport":"KEF"
                            },
                            "marketing_airline":"WW",
                            "operating_airline":"WW",
                            "flight_number":"104",
                            "aircraft":"321",
                            "booking_info":{  
                               "travel_class":"ECONOMY",
                               "booking_code":"X",
                               "seats_remaining":8
                            }
                         },
                         {  
                            "departs_at":"2018-09-26T06:10",
                            "arrives_at":"2018-09-26T10:25",
                            "origin":{  
                               "airport":"KEF"
                            },
                            "destination":{  
                               "airport":"LGW",
                               "terminal":"S"
                            },
                            "marketing_airline":"WW",
                            "operating_airline":"WW",
                            "flight_number":"810",
                            "aircraft":"321",
                            "booking_info":{  
                               "travel_class":"ECONOMY",
                               "booking_code":"X",
                               "seats_remaining":9
                            }
                         }
                      ]
                   },
                   "inbound":{  
                      "duration":"11:05",
                      "flights":[  
                         {  
                            "departs_at":"2018-09-27T17:20",
                            "arrives_at":"2018-09-27T19:30",
                            "origin":{  
                               "airport":"STN"
                            },
                            "destination":{  
                               "airport":"KEF"
                            },
                            "marketing_airline":"WW",
                            "operating_airline":"WW",
                            "flight_number":"827",
                            "aircraft":"321",
                            "booking_info":{  
                               "travel_class":"ECONOMY",
                               "booking_code":"E",
                               "seats_remaining":9
                            }
                         },
                         {  
                            "departs_at":"2018-09-27T21:10",
                            "arrives_at":"2018-09-27T23:25",
                            "origin":{  
                               "airport":"KEF"
                            },
                            "destination":{  
                               "airport":"JFK",
                               "terminal":"1"
                            },
                            "marketing_airline":"WW",
                            "operating_airline":"WW",
                            "flight_number":"109",
                            "aircraft":"321",
                            "booking_info":{  
                               "travel_class":"ECONOMY",
                               "booking_code":"E",
                               "seats_remaining":9
                            }
                         }
                      ]
                   }
                }
             ],
             "fare":{  
                "total_price":"544.61",
                "price_per_adult":{  
                   "total_fare":"544.61",
                   "tax":"353.61"
                },
                "restrictions":{  
                   "refundable":false,
                   "change_penalties":true
                }
             }
          }
       ]
    }

我正在尝试遍历JSON并从像这样的一些键中获取值

    $arr = json_decode($data,true);
foreach($arr['results'] as $data) {
            echo "duration: ".$data['itineraries']['outbound']['duration'].PHP_EOL.
                "origin: ".$data['itineraries']['outbound']['flights']['origin'].PHP_EOL; //etc
    }

但是,我在执行它时没有遇到任何错误,但是我也没有从键中获取值。 我正在尝试获取持续时间和原始密钥的值。知道我在做什么错吗?

刚发现我的错误。我忘记了[0]。这样就解决了。

foreach($arr['results'] as $data) {
        echo "duration: ".$data['itineraries'][0]['outbound']['duration'].PHP_EOL.
            "origin: ".$data['itineraries'][0]['outbound']['flights'][0]['origin']['airport'].PHP_EOL; //etc
}

1 个答案:

答案 0 :(得分:1)

某些已解析的元素在数组内。因此,您必须在某些位置添加[0]。 这将起作用:

foreach ($arr['results'] as $data) {
    echo "duration: " . $data['itineraries'][0]['outbound']['duration'] . PHP_EOL .
        "origin: " . $data['itineraries'][0]['outbound']['flights'][0]['origin']['airport'] . PHP_EOL; //etc
}

输出:

duration: 07:10
origin: EWR
duration: 10:30
origin: EWR