Я пытаюсь получить некоторые значения из вложенного 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
}