回声部分json在php中返回

my returned json looks like this http://pastebin.com/Nbr161s3

I want to echo

body->airTicketListResponse->routings->mainAirlineName
body->airTicketListResponse->routings->adultBasePrice
body->airTicketListResponse->routings->trips->segments->departureAirportCode
body->airTicketListResponse->routings->trips->segments->departureTime //only the time here
body->airTicketListResponse->routings->trips->segments->duration

for each routings.

How do I do this? Here is what I have but I am lost and I know it is way off.

$result = data returned here http://pastebin.com/Nbr161s3
$airTicketListResponse = $result->body->airTicketListResponse;
$routings = $result->body->airTicketListResponse->routings;
$trips = $result->body->airTicketListResponse->routings->trips;
$segments = $result->body->airTicketListResponse->routings->trips->segments;

foreach($airTicketListResponse as $item){
    $i=0; 
    $i<count($routings); 

    echo '<span style="font-weight:bold;">Airline - '.$item->routings[i]->mainAirlineName.' Price - '.$item->routings[i]->adultBasePrice.'</span><br />'.$item->routings[i]->trips[i]->segments[i]->departureAirportCode.' '.$item->routings[i]->trips[i]->segments[i]->departureTime.'<br /><br />';
    $i++;
    }

Please help if you can.

Before working with JSON you should be familiar with working with arrays and objects since JSON is nothing more than that.

It seems you already know these two concepts

  1. To access an object property in PHP you use obj->property
  2. To access a value of an array you specify the index inside brackets array[0]

With JSON you just have to keep in mind that some of your object properties will be arrays.

Now, since your data comes in a multi-level three-like structure you should also be familiar with traversing arrays, PHP offers an implementation of a foreach loop that is ideal for traversing dynamically generated arrays

using foreach($array as $index => $var) the $index and $var variables are automatically set to the index and value of each element in the array as they are being traversed, so you don't manually need to keep track of the index (i.e. $i)

Now let's start going through your data:

First we find your routings array

$result = json_decode($data);
$airTicketListResponse = $result->body->airTicketListResponse;
$routings = $airTicketListResponse->routings;

Now we use foreach to loop through every routing and print the needed properties

foreach($routings as $routing){ //$routing will hold the object value in each loop
    echo 'Airline '.$routing->mainAirlineName.'<br>';
    echo 'Adult Base Price '.$routing->adultBasePrice.'<br>';
}

Getting single properties like the above is pretty straight forward, but for the information of the segments we would first need a nested foreach since we have multiple trips for each routing and then a second nested foreach since each trip has multiple segments

foreach($routings as $routing){ //$routing will hold the object value in each loop
    echo 'Airline '.$routing->mainAirlineName.'<br>';
    echo 'Adult Base Price '.$routing->adultBasePrice.'<br>';
    foreach($routing->trips as $trip){
        foreach($trip->segments as $index => $segment){
            echo 'Segment '.$index.':<br>'
            echo 'Depart From '.$segment->departureAirportCode.'<br>';
            echo 'Departure Time '.$segment->departureTime.'<br>';
            echo 'Duration '.$segment->duration.'<br>';
        }
    }
}

And that would be it. I hope my explanation was clear and you got the idea of how to traverse JSON objects

If you feel more comfortable working with an array than with an object to access your data [which might be where you're getting confused here] then you can use json_decode with an additional argument:

$data = json_decode($result, true); 

This will leave you with an array ($data) containing all your flight information, you can then var_dump() it and see the hierarchy you're dealing with and loop through it.