目标
使用$context
作为PHP
中的stdClass
的属性名称。
场景
在我的应用程序中有以下场景:
[...]
public function diagnose($issue, $index)
{
$json = json_decode($this->getJson());
$json->$issue[$index]
}
[...]
实例:
$investigator = new Investigator;
$investigator->diganose('parameters', 0);
JSON:
{
"parameters": [
"There is missing a parameter."
]
}
错误
注意:中未定义的属性:stdClass::$p[…]在25号线上
因此,我想灵活地访问JSON对象的parameters
。我该如何执行此操作?
只需尝试:
public function diagnose($issue, $index)
{
$json = json_decode($this->getJson());
$data = $json->$issue;
return $data[$index];
}