我正在使用 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
)
)
)
)
我想访问“城市”键下@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).
关于php - 如何访问 $array[@key] 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29279592/