2

可能重复:
名称中带有点的 php 对象属性

我正在处理 PHP,获取 Microsoft Web 服务返回的对象,并且对象名称中有句点!

    object(stdClass)#22 (1) {
  ["DAE.Country"]=>
  array(24) {
    [0]=>
    object(stdClass)#23 (2) {
      ["CountryName"]=>
      string(4) "Asia"
      ["ID"]=>
      string(2) "27"
    }
}
}

如何在 PHP 中访问名称中带有句点的对象?

$response->DAE_GetCountryListResult->DAE.Country;

$response->DAE_GetCountryListResult-['DAE.Country'];

都失败了。感谢您的时间。

4

1 回答 1

4

您可以使用此语法来访问所需的属性:

$obj->{'DAE.Country'}

您还可以在大括号内使用变量和表达式:

$prefix = 'DAE';
$name = 'Country';
$another_obj = $obj->{"$prefix.$name"};
于 2012-10-24T14:19:51.757 回答