简体   繁体   English

如何访问$ array [@key]值

[英]How to access $array[@key] value

I am working with expedia API's and its working well but I don't know how to access this special type of array key. 我正在使用expedia API并且它运行良好,但我不知道如何访问这种特殊类型的数组键。 Response is given 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 value of @hotelId under the 'city' key but I can't 我想在'city'键下访问@hotelId的值,但我不能

I tried with both type but failed both time as 我试过两种类型,但两次失败都没有

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

Please help me.. thank you in advance 请帮帮我..提前谢谢你

This should work for you: 这应该适合你:

(This is because you can't access a property which doesn't have a legal variable name, so you have to use curly syntax) (这是因为您无法访问没有合法变量名称的属性,因此您必须使用卷曲语法)

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

You can read more about this in the manual: http://php.net/manual/en/language.variables.variable.php 您可以在手册中阅读更多相关信息: http//php.net/manual/en/language.variables.variable.php

And a quote from there: 从那里引用:

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 (eg from json_decode() or SimpleXML). 当访问包含数组的属性中的值,属性名称由多个部分组成时, 或者属性名称包含其他方式无效的字符时 (例如,来自json_decode()或SimpleXML),它们最有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM