2

I want to display the Itinerary details from developer.ean.com API. By passing the customer's Itinerary ID and Email ID I got the details of reservation. The result is comming in json format so I decoded it and creating array by using :

$array=json_decode($result);

The problem is whatever the result comming from API contain problem like :

For some records it providing array like this:

[Itinerary] => stdClass Object
        (
            [HotelConfirmation] => Array
                (
                    [0] => stdClass Object
                        (
                            [supplierId] => 13
                            [chainCode] => EP
                            [arrivalDate] => 07/24/2012
                            [departureDate] => 07/26/2012
            )
            [Hotel] => Array
                        (
                            [0] => stdClass Object
                                (
                                   [hotelId] => 394808
                                   [statusCode] => A
                                   [name] => Barkston Youth Hostel
                )
            )
        )
    )

In this case the HotelConfirmation and Hotel is Array which contain [0] as object

and for some records it providing array like this:

[Itinerary] => stdClass Object
        (
            [HotelConfirmation] => stdClass Object
                (
                            [supplierId] => 13
                            [chainCode] => EP
                            [arrivalDate] => 07/24/2012
                            [departureDate] => 07/26/2012
            )
       [Hotel] => stdClass Object              
                (
                            [hotelId] => 394808
                            [statusCode] => A
                            [name] => Barkston Youth Hostel
            )
       )

and In this case the HotelConfirmation and Hotel is itself an object

I providing only few data here actually its big array and I want to provide list of it. But the array containing ambiguity like this. How can I handle this issue. Is there any solution.

Thanks in advance.

8 Answers 8

2

Pass true as the second argument to json_decode. This will create an array instead of stdClass

$array=json_decode($result, true);
Sign up to request clarification or add additional context in comments.

4 Comments

What is your original json string?
sometime its like HotelConfirmation":{"supplierId":13} and some time its like HotelConfirmation":[{"supplierId":13}]
Is there any pattern to when each one occurs?
No it's occuring for any record
2

you can normalize the input like so:

// get objects as arrays
$array = json_decode($result, true);
// remove the sub-level [0], when necessary
foreach ($array as $key => $value) {
    if (is_array($value[0])) {
        $array[$key] = $value[0];
    }
}

Then the result always looks the same:

[Itinerary] => Array
        (
            [HotelConfirmation] => Array
                (
                            [supplierId] => 13
                            [chainCode] => EP
                            [arrivalDate] => 07/24/2012
                            [departureDate] => 07/26/2012
            )
            [Hotel] => Array              
                (
                            [hotelId] => 394808
                            [statusCode] => A
                            [name] => Barkston Youth Hostel
            )
       )

2 Comments

@Kunal: can you post a complete JSON result somewhere so i can test with?
Thanks...Actually I made some adjustment with the help of your code....thank you very much bro
1

First do this:

$array = json_decode($result, true);

Which will convert objects into associative arrays

And do adjustment like this:

if (isset($array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'][0])) {
    $array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'] = $array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'][0];
}

It will definitly work.

Comments

0

You can use is_array() to check for it:

if (is_array($hotel)) {
    $hotel = $hotel[0];
}

Comments

0

just change to this:

$array=json_decode($result,TRUE);

and handle arrays always?

Comments

0

Looks like you may have to account for both possibilities in your model... Checking to see if the hotel node contains and array or an obj and operating accordingly.

Comments

0

you can type case the object inside of an array.

$array = json_decode($result);
$array = (array)$array

or alternatively you can pass true as the second argument in your json_decode();

according to php documentation

When TRUE, returned objects will be converted into associative arrays.

$array = json_decode($result, true);

Comments

0

Check for object or array:

if( is_object($Itinerary -> HotelConfirmation)) {
// do one thing or nothing
} elseif( is_array($Itinerary -> HotelConfirmation)) {
$Itinerary -> HotelConfirmation = array_shift( $Itinerary -> HotelConfirmation );
}

Comments

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.