0

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.

6
  • 3
    use file_get_contents to load a url, then json_decode to decode it Commented Aug 5, 2013 at 18:58
  • and json_encode() to re-encode the part you want to echo. Commented Aug 5, 2013 at 19:01
  • @sgroves I think he has that part covered, the issue is with traversing the JSON object and printing the values he needs Commented Aug 5, 2013 at 19:07
  • guys i think you are missing it and it is my fault. The url for the data is just there so you can see what is returned to me. I actually get the data returned to the $results var already. Commented Aug 5, 2013 at 19:07
  • @koala_dev yes you are correct. Can you rescue me? I know you have given me great help in the past! Commented Aug 5, 2013 at 19:08

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

WOW what a great explanation! So the as $index=>$segment takes care of the [i] i was trying to do. I will hold on to this forever! Thanks koala_dev!
spoke to soon again. If i try to json_decode i get nothing returned with above code. If i dont do json_decode I get one result set of mainAirlineName and adultBasePrice only...nothing else.
ok fixed changed foreach($routing->$trips as $trip){.....to foreach($routing->trips as $trip){
Oops that $ slipped, I have fixed the answer. I'm glad you found the explanation to be clear. Best of luck with your project @smitty :)
0

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.

1 Comment

when I do this and var_dump($data) it comes back null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.