0

I am trying to get some values from a nested JSON, but for some reason I am not getitng the values of the keys. This is my 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
                }
             }
          }
       ]
    }

I am trying to foreach trough the JSON and get the value from some of the keys like this

    $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
    }

However, I am not getting any errors while executing it, but I am not getting the values from the keys either. I am trying get the values for the duration and origin keys. Any idea what I am doing wrong?

Just found my mistake. I forgot the [0]. So this fixed it.

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
}
lStoilov
  • 1,256
  • 3
  • 14
  • 30
  • 2
    Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – Andreas Sep 09 '18 at 04:56

1 Answers1

1

Some parsed elements are within an array. And therefore you must add [0] on some spots. This will work:

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
}

Output:

duration: 07:10
origin: EWR
duration: 10:30
origin: EWR
EvE
  • 734
  • 3
  • 13