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时,我会得到“注意:试图获取非对象属性”错误。
发布于 2012-06-15 20:38:12
如果尚未开始遍历数组,则可以使用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()获取数组的第一个元素,但当您这样做时,它将从数组中删除该元素。
这些函数都不关心键类型。
https://stackoverflow.com/questions/11057821
复制相似问题