我正在尝试创建一个包含以下图层的php对象。
$obj->property->name;
$obj->property->title;
$obj->property->id;
$obj->property->height;
它给了我' Trying to get property of non-object
'错误
我的对象
$obj = [
'property' => [
'name' => 'Rick',
'title' => 'manager',
'id' => '123',
'height' => '5.6'
]
];
$obj = = (object)$obj;
我不确定生成$obj->property->name;
的正确语法任何人都可以帮助我吗?我的大脑被解雇了......谢谢!
答案 0 :(得分:1)
property object
中有一个你不能像那样使用的数组
$obj->property->name;
这应该是:
$obj->property['name'];
示例:强>
$yourArr = array('property'=>array(
'name' => 'Rick',
'title' => 'manager',
'id' => '123',
'height' => '5.6'
));
$yourObj = (object) $yourArr;
echo $yourObj->property['name']; // Rick
答案 1 :(得分:1)
你应该转为object
property
:
$obj = [
'property' => [
'name' => 'Rick',
'title' => 'manager',
'id' => '123',
'height' => '5.6'
]
];
$object = (object)$obj;
$object->property = (object)$object->property;
结果:
var_dump($object->property->id); // string(3) "123"
var_dump($object->property->name); // string(4) "Rick"
// etc.