如何在嵌套数组/对象中获取此信息


How do I get at this piece of info in a nested array/object?

print_r($rows)返回以下内容:

Array
(
    [S1 | Excellence in P-O-P Execution: Ripping Down the Roadblocks to Breakthrough In-Store Marketing] => Array
        (
            [group] => S1 | Excellence in P-O-P Execution: Ripping Down the Roadblocks to Breakthrough In-Store Marketing
            [rows] => Array
                (
                    [0] => stdClass Object
                        (
                            [nid] => 207
                            [node_title] => Excellence in P-O-P Execution: Ripping Down the Roadblocks to Breakthrough In-Store Marketing
                            [taxonomy_term_data_field_data_field_track_term_tid] => 19
                            [node_field_data_field_speaker_title] => Jon Kramer
                            [node_field_data_field_speaker_nid] => 205
                            [field_data_field_date_time_field_date_time_value] => 2012-10-16 18:00:00
                            [field_data_field_session_number_field_session_number_value] => S1
                            [field_data_field_date_time_node_entity_type] => node
                            [field_data_field_session_number_node_entity_type] => node
                            [field_data_field_track_icon_taxonomy_term_entity_type] => taxonomy_term
                            [field_data_field_job_title_node_entity_type] => node
                            [field_data_field_company_node_entity_type] => node
                            [field_data_field_hide_track_node_entity_type] => node

(我知道我错过了所有的结尾部分;返回实际上有几千行长,我只是懒得遍历并找到所有内容。

我将如何获取名为nid的数据?我原以为会是

$rows[0]['rows'][0]->nid

但是我得到一个未定义的偏移错误。我绝对无法使用完整内容访问数组的第一级(S1 |卓越等)- 这是动态生成的。我曾想过,因为它是数组的第一个元素,我可以用零偏移量来获得它,但显然不是。

更新

根据下面的答案,我已经尝试了一些使用 current() 的方法; 它让我更接近一个级别,但我仍然无法访问 nid 元素。

$row = current($rows);
$nid_tmp = $row['rows'];
print '<pre>'; var_dump($nid_tmp); print '</pre>';

返回

Array
(
    [0] => stdClass Object
        (
            [nid] => 207

很好;这就是我所期待的。但是当我尝试print $nid_tmp[0]->nid时,我收到"注意:尝试获取非对象的属性"错误。

如果你还没有开始遍历数组,你可以使用 current() 来获取第一个元素。 http://us2.php.net/manual/en/function.current.php

$row = current($rows); // returns the first element of the array
$firstObject = $row['rows'][0];
$nid = $firstObject->nid;

或者你可以使用 reset() 来倒带指针并获取第一个元素。http://us2.php.net/manual/en/function.reset.php

$row = reset($rows);

您也可以使用 array_shift() 获取数组的第一个元素,但当您这样做时,它会从数组中删除该元素。

这些函数都不关心密钥类型。