How to access $ array [@key] value

I work with the API and its functionality, but I do not know how to access this type of keys of a special type. The answer is below.

$response = 
stdClass Object
(
    [@size] => 1
    [@activePropertyCount] => 144
    [city] => 1
    [@hotelId] => 12345
    [HotelSummary] => stdClass Object
        (
            [@order] => 0
            [@ubsScore] => 1074874
            [hotelId] => 151689
            [RoomRateDetailsList] => stdClass Object
                (
                    [RoomRateDetails] => stdClass Object
                        (
                            [roomTypeCode] => 195577
                            [rateCode] => 202369379
                            [maxRoomOccupancy] => 3
                            [quotedRoomOccupancy] => 2
                            [minGuestAge] => 0
                            [roomDescription] => Deluxe Room
                            [propertyAvailable] => 1
                            [propertyRestricted] => 
                            [expediaPropertyId] => 526332
                        )
                )
        )
)

I want to access the @hotelId value under the key "city", but I can’t

I tried using both types, but did not execute both times as

$response->hotelId
and 
$response->@hotelId

Please help me .. thanks in advance

+4
source share
1 answer

This should work for you:

(This is because you cannot access a property that does not have a legal variable name, so you need to use curly syntax)

echo $response->{"@hotelId"};

: http://php.net/manual/en/language.variables.variable.php

:

, . , , , , (, json_decode() SimpleXML).

+5

All Articles