从标准类对象访问元素


Accessing element from standard class object

我试图检索Drupal模块Viewsphp中的变量,但我的问题真的只是访问stdclass对象中的嵌套元素。

print_r($data->node_created);//给出正确值1477420603

print_r ($ data -> _field_data - ->实体>>国家免疫日vid);

我做错了什么?

下面是返回数据的摘录:

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

首先使用对象操作符->。这就是访问对象的属性或方法的方式。这将返回该操作的值。

//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的变化。

的文档或代码值得一看。