2013-01-13 87 views
0

任何想法爲什麼會發生這種情況?PHP數組元素直接訪問時返回null

代碼:

<?php 
print_r($this->$property); 
var_dump($this->$property[0]); 
?> 

輸出:

Array 
(
    [0] => WP_Post Object 
     (
      [ID] => 34901 
      [post_author] => 1 
      [post_date] => 2013-01-04 21:04:34 
      [post_date_gmt] => 2013-01-05 02:04:34 
      [post_content] => 
      [post_title] => Castro Theater 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => castro-theater 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2013-01-04 21:04:34 
      [post_modified_gmt] => 2013-01-05 02:04:34 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://demo.gala.local/2012/venues/castro-theater/ 
      [menu_order] => 0 
      [post_type] => venue 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
      [p2p_id] => 34444 
      [p2p_from] => 34891 
      [p2p_to] => 34901 
      [p2p_type] => scheduleitem_to_venue 
     ) 

) 
NULL 
+0

如果您正在訪問一個類的屬性,你shoudl'$此 - >屬性[0]' – Starx

+0

請不要用犯規的話,以避免被標記。 – Starx

+0

@Starx \t $屬性是包含屬性,而不是物業本身的名稱的變量。 – jessica

回答

1

你的代碼最終處理加工$property[0]第一,然後嘗試你的對象的屬性與該結果寄存。如果您首先獲得$this->$property,那麼您可以使用普通數組表示法獲取事物,並且事情將按照您的期望工作。

$data = $this->$property; 
var_dump($data[0]); 
+0

啊,我明白了。謝謝!我已經通過使用'$ this - > {$ property} [0]'修復了它 – jessica