获取包含斜杠的对象属性


Getting an objects property with slashes in it

我正在从对 freebase 数据库的 api 请求中返回一个 json 结果。这是返回的名为 $json 的对象的一部分。$json的变量转储:

stdClass Object
(
[name] => Abomey
[/location/statistical_region/population_growth_rate] => 
[/common/topic/article] => Array
    (
        [0] => stdClass Object
            (
                [id] => /m/0jk2c
            )
    )

如何减去/m/0jk2c 部分?

$json->/common/topic/article[0]->id(显然)不起作用。

这应该可以做到:

$json->{"/common/topic/article"}[0]->id

这是你应该使用的

 $class->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id

您的对象看起来像这样的原因

$std = new stdClass();
$std->id = '/m/0jk2c' ;
$json  = new stdClass();
$json->name = "Abomey" ;
$json->{'/location/statistical_region/population_growth_rate'} = array('/common/topic/article'=>array($std));

如果您运行

var_dump($json->{'/location/statistical_region/population_growth_rate'}['/common/topic/article'][0]->id);

输出

string '/m/0jk2c' (length=8)

echo "<pre>";
print_r($json);

输出

stdClass Object
(
    [name] => Abomey
    [/location/statistical_region/population_growth_rate] => Array
        (
            [/common/topic/article] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => /m/0jk2c
                        )
                )
        )
)