1

I am having trouble parsing this data set that I have posted at pastebin here http://pastebin.com/TmZGw92j

I can step into it as far as routings but then can not go any further for some reason. Here are my vars that I have set up:

$airTicketListResponse = $result->body->airTicketListResponse;
$routings = $airTicketListResponse->routings;
$trips = $routings->trips;
$segments = $trips->segments;

I can print_r($routings) but when I try to print_r($segments) I get nothing returned. I would like to pull items from routings and segments.

here is my current foreach loop that craps out at trips.

foreach($routings as $item){
    echo '<span style="font-weight:bold;">Airline - '.$item->mainAirlineName.' Price - '.$item->adultBasePrice.'</span><br />'.$item->trips->segments->departureAirportCode.' '.$item->trips->segments->departureTime.'<br /><br />';

    }
3
  • The JSON in raw_body:Unirest\HttpResponse:private seems invalid, you're missing two } at the end. Commented Aug 5, 2013 at 15:20
  • First off, use var_dump instead of print_r Make sure that error level is set to maximum, i.e. error_reporting(2047); so you will see any notices that could help. Commented Aug 5, 2013 at 15:22
  • yes i missed that in the paste...it is valid though. So how would I get to segments? Commented Aug 5, 2013 at 15:37

2 Answers 2

2

The trips and segments elements are arrays of objects, not a single object.

So you need to reference the array element [0] which part of the structure.

$trips = $routings[0]->trips;
$segments = $trips[0]->segments;

Note, that there appear to be two trips, you'll also need $trips[1]->segments if you want all the segments.

More likely, you'll be wanting to use foreach() loops to read them, rather than directly referencing the array keys.

Something like this?

foreach($routings as $routing) {
    $trips = $routing->trips;
    .... do something here with $trips? ....
    foreach($trips as $trip) {
         $segments = $trip->segments;
         .... do something here with $segments? ....
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Spudley so what would the foreach loop look like to access items in routings and segments?
@smitty - I've edit the answer to include some stub code. you'll obviously need to do more than that, but exactly what you do with it depends on what your program is doing, so I can't help with that.
@Spudley thanks bud but dont I need to do i++ and do something like routings[i]segments[i] or something? That is where I lose my mind? Right now if I just display $routings[1] it just shows 1 record.
There shouldn't need to be any ++ with the code I've put above, but there might be with some other ways of doing it. You clearly need more help with this than I have time to give. Plus we're going a long way beyond "how to process json"; this is more like "basic PHP 101". I can only suggest taking it slowly and reading what the PHP error messages are telling you (make sure you have full error reporting enabled). If you have further more specific questions, try making a separate post for the particular bit you're struggling with.
@Spudley when you assign the var like this $trips = $routings[0]->trips; it only gets one instance(the very first).
0

You have a few layers of arrays nested in there. I think your code needs to change to this:

$trips = $routings[0]->trips;
$segments = $trips[0]->segments;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.