如何访问 $array[@key] 值

How to access $array[@key] value

我正在使用 expedia API 并且它运行良好,但我不知道如何访问这种特殊类型的数组键。回复如下

$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
                        )
                )
        )
)

我想访问 'city' 键下@hotelId 的值,但我不能

我尝试了两种类型,但都失败了,因为

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

请帮助我..提前谢谢你

这应该适合你:

(这是因为你不能访问一个没有合法变量名的属性,所以你必须使用curly语法)

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

您可以在手册中阅读更多相关信息:http://php.net/manual/en/language.variables.variable.php

引自那里:

Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of mulitple parts, or when the property name contains characters that are not otherwise valid (e.g. from json_decode() or SimpleXML).