2012-06-15 92 views
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,我得到的通知「:試圖讓非對象的財產」的錯誤。

回答

2

如果你還沒有開始通過數組循環,您可以使用電流()來獲得的第一個元素。 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; 

或者您可以使用重置(),倒轉指針和獲得的第一個元素。 http://us2.php.net/manual/en/function.reset.php

$row = reset($rows); 

您還可以使用array_shift()來得到一個數組的第一個元素,但是當你做到這一點,將刪除該元素的數組。

這些函數都不關心的密鑰類型。

+0

所以後來我怎麼的元素?我假設我不能做像'current($ rows)[0] - > nid';是否必須將'current($ rows)'的結果設置爲另一個變量,然後從那裏開始? – EmmyS

+0

我完全陷在這裏。我正在更新OP,以顯示迄今爲止我嘗試過的內容以及給出的結果。 – EmmyS

+0

我已添加到我的答案。我想你在第一條評論中錯過了一個級別。也許。誰做這個陣列不是你的朋友。 :) –

相關問題