我正在使用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
请帮帮我..提前谢谢你
解决方法:
这应该适合你:
(这是因为您无法访问没有合法变量名称的属性,因此您必须使用卷曲语法)
echo $response->{"@hotelId"};
从那里引用:
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,arrays,array-key,arrayobject
来源: https://codeday.me/bug/20190609/1205456.html