在php中访问此STDCLASS对象

时间:2013-04-15 06:36:05

标签: php arrays oop curl stdclass

如何在stdclass中使用@符号访问对象我在php中使用curl尝试了这个: $ response-> HotelListResponse-> customerSessionId(这有效),但当我这样做时: $ response-> HotelListResponse-> HotelList-> @activePropertyCount返回错误或空字符串

stdClass Object

[HotelListResponse] => stdClass Object
    (
        [customerSessionId] => 0ABAA84E-3DC0-4913-E0C2-5C6541908000
        [numberOfRoomsRequested] => 1
        [moreResultsAvailable] => 1
        [cacheKey] => 6b03dc04:13e0c5c6541:-7ffd
        [cacheLocation] => 10.186.168.78:7302
        [cachedSupplierResponse] => stdClass Object
            (
                [@matchedLocale] => true
                [@matchedCurrency] => true
                [@tpidUsed] => 5001
                [@otherOverheadTime] => 2
                [@candidatePreptime] => 13
                [@supplierResponseTime] => 680
                [@supplierResponseNum] => 25
                [@supplierRequestNum] => 207
                [@cachedTime] => 0
                [@supplierCacheTolerance] => NOT_SUPPORTED
            )

        [HotelList] => stdClass Object
            (
                [@activePropertyCount] => 224
                [@size] => 25
                [HotelSummary] => Array
                    (
                        [0] => stdClass Object
                            (
                                [@ubsScore] => 6144465
                                [@order] => 0

1 个答案:

答案 0 :(得分:1)

您可以通过两种方式完成此操作。

方法1:

使用Mark Ba​​ker建议使用花括号的相同方法调用它。大括号用于明确指定变量名称的结尾。

$result->HotelListResponse->HotelList->{'@activePropertyCount'};

您可以参考此链接获取更多信息http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

方法2:

@activePropertyCount分配给某个变量。你可以这样做:

$var = '@activePropertyCount';

然后你可以从

得到结果
$result->HotelListResponse->HotelList->$var;

希望这可以帮助你:)