0

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 | Excellence 等)访问数组的第一级 - 这是动态生成的。我曾想过,因为它是数组的第一个元素,我可以用零偏移量得到它,但显然不是。

更新

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,我得到“注意:试图获取非对象的属性”错误。

4

1 回答 1

2

如果你还没有开始遍历数组,你可以使用 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() 来获取数组的第一个元素,但这样做时它会从数组中删除该元素。

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

于 2012-06-15T20:38:12.270 回答