我试图在Drupal Module Viewsphp中检索一个变量,但我的问题实际上只是访问stdclass对象中的嵌套元素。
print_r($ data-> node_created); //给出正确值1477420603
的print_r($ DATA-> _field_data-> nid->实体 - > VID); 什么都不应该返回31
我做错了什么?
以下是返回数据的摘录:
stdClass Object
(
[node_title] => Denver
[nid] => 31
**[node_created] => 1477420603**
[field_data_body_node_entity_type] => node
[field_data_field_colour_node_entity_type] => node
[field_data_field_type_node_entity_type] => node
[_field_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
**[vid] => 31**
[uid] => 1
[title] => Denver
[log] =>
[status] => 1
[comment] => 2
[promote] => 1
[sticky] => 0
[nid] => 31
[type] => test1
[language] => und
答案 0 :(得分:1)
您最初使用对象操作符->
时正在使用对象。这是您访问对象的属性或方法的方式。这将返回该操作的值。
//This is accessing the array stored in _field_data
$data->_field_data;
//Since that is an array now you have to access
//the data in it with the indexes of the array
$data->_field_data['nid']['entity'];
请注意,虽然您的输出[entity] => stdClass Object
实体已返回对象,因此您需要返回->
。
//Full access
$data->_field_data['nid']['entity']->vid;
通常,对象具有访问器或getter方法,即getVid()方法。不确定这是否是这种情况,但是你可以访问$data->getVid();
之类的数据,这样可以简单得多,如果底层api发生变化,可以保护你的代码。值得研究文档或代码。