1

I am using the following code to print the output of the jSON response but when I try to print

echo $obj->HotelListResponse->customerSessionId;  // This is working.

echo $obj->HotelListResponse->HotelList->HotelSummary->name; // This is not working.

When the response contains only one node then its printing perfectly but when there are multiple nodes with same name then its not printing. I tried using foreach just like the below. I also tried using while loop but still I am unable to print the list of hotel names.

My jSON decoded output is like http://pastebin.com/Fr21DkEk

Total code:

$url = "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_AU&currencyCode=AUD&xml=<HotelListRequest><city>Brisbane</city><stateProvinceCode>QLD</stateProvinceCode><countryCode>AU</countryCode><arrivalDate>10/16/2014</arrivalDate><departureDate>10/18/2014</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup><numberOfResults>25</numberOfResults></HotelListRequest>";
$json = file_get_contents($url);
$obj = json_decode($json);

foreach($obj as $val) {
echo $val->HotelListResponse->HotelList->HotelSummary->name;
}

3 Answers 3

1

Try this

foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
    echo $val->name . '<br/>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

How to print only first value without printing all?
@lock, maybe this: $first = true; foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) { if ( $first ) { // do something $first = false; } else { // do something } // do something }
1

HotelSummary is an array:

echo $val->HotelListResponse->HotelList->HotelSummary[0]->name;

If you want all of the hotel summaries:

foreach($obj as $val) {
    foreach($val->HotelListResponse->HotelList->HotelSummary as $sum) {
        echo $sum->name;
    }  
}

2 Comments

no, still not working. Warning: Invalid argument supplied for foreach()
I question the json you have posted then.
1

Yes you can directly access them inside the foreach. Like this:

foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
             // ^^
    // since you're interested on just names, you can point it directly on that object, then each of that batch is in `$val`
    echo $val->name . '<br/>';
}

// or start from the parent
foreach($obj as $values) {

    $customerSessionId = $values->customerSessionId;
    echo $customerSessionId . '<hr/>';
    $hotelList = $values->HotelList;
    foreach($hotelList->HotelSummary as $hotelsummary) {
        echo $hotelsummary->name . '<br/>';
    }
}

4 Comments

How to print only first value without printing all?
@lock can you paraphrase? what is your goal? and what is the first value anyway? can you provide more context? its unclear.
@lock if you want the first value only, you could directly point it out to the first index, or use break; after echoing
@lock sure mate no prob

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.