我正在使用航空公司航班搜索,并在回显print_r($ result)时获得以下输出。
Unirest\HttpResponse Object
(
[code:Unirest\HttpResponse:private] => 200
[raw_body:Unirest\HttpResponse:private] => {"airTicketListResponse":{"customerSessionId":"1373316267","currency":"USD","routings":[{"key":"Xt4yT/xiADHlJY1rDoGfIW1oqil8CPCWFIlJLLRPgb8^vJwIcDs7qiScjgw^YEk5DvQVRZ1Ge1ut9cq4GDemu8aQQ1r4LqOr3z3asbm/3xcR9G4YzjkKns6CoqNxFEuO8rCrifO9GnKWSQaFrOWE/JRLlKewmNAAGIz41wbgI4BYVBeiWfbPO7v/EDI3y92fKShWVCKarRbMUq4KZ6p^i7ROySEM1S9Kw6pu7OXYgn9NUpIWd1ReV0X^32AROvy^uVtRFCYMKkplFEEEafGVivdQXwii7Be/6Hpmj71D4QKNZsoPzHqH9cehD6123fqhmCkVzSTED3z^B79K/o^3ezOaf73EgLLg8eVB7pqTTk0=","mainAirlineCode":"DL","mainAirlineName":"Delta Airlines","totalPrice":"231.80","adultBasePrice":"196.04","adultTax":"35.76","childBasePrice":"0","childTax":"0","lastTicketDate":"08/05/2013","domesticTicket":true,"duration":"180","tripTime":"180","layover":false,"operatedBy":false,"overnightFlight":false,"overnightStay":false,"publishFare":true,"trips":[{"segments":[{"airlineCode":"DL","airlineName":"Delta Airlines","departureAirportCode":"PHX","departureAirportName":"Sky Harbor International Airport","departureTime":"11/22/2013 06:00","arrivalAirportCode":"LAX","arrivalAirportName":"Los Angeles International Airport","arrivalTime":"11/22/2013 06:35","stop":0,"duration":95,"codeShare":"","cabin":"E","aircraftCode":"Canadair RJ","flightNumber":"4578","departureCity":"Phoenix","departureStateCode":"AZ","departureCountryCode":"US","departureCountryName":"United States","arrivalCity":"Los Angeles","arrivalStateCode":"CA","arrivalCountryCode":"US","arrivalCountryName":"United States","BookingClass":"T","MarriageGroup":"O"}],"duration":"95","tripTime":"95"},{"segments":[{"airlineCode":"DL","airlineName":"Delta Airlines","departureAirportCode":"LAX","departureAirportName":"Los Angeles International Airport","departureTime":"12/22/2013 08:30","arrivalAirportCode":"PHX","arrivalAirportName":"Sky Harbor International Airport","arrivalTime":"12/22/2013 10:55","stop":0,"duration":85,"codeShare":"","cabin":"E","aircraftCode":"Canadair RJ","flightNumber":"4793","departureCity":"Los Angeles","departureStateCode":"CA","departureCountryCode":"US","departureCountryName":"United States","arrivalCity":"Phoenix","arrivalStateCode":"AZ","arrivalCountryCode":"US","arrivalCountryName":"United States","BookingClass":"X","MarriageGroup":"O"}],"duration":"85","tripTime":"85"}]},
如何循环显示此内容并为返回的每个项目回显mainAirlineName和adultBasePrice?我不知道这是一个数组内部的数组,还是我通过print_r($ result)搞砸了什么。
答案 0 :(得分:3)
//Decode the json string to an object
$data = json_decode($result);
//Now access the properties by their names
$TicketList = $data->airTicketListResponse;
$Routings = $TicketList->routings;
//$Routings is now array so use array notation to use it
echo $Routings[0]->adultTax;
//Looping over $Routings
foreach($Routings as $Route){
echo $Route->adultTax;
}
等等。