PHP数组元素在直接访问时返回null

时间:2013-01-13 03:06:51

标签: php arrays wordpress var-dump

知道为什么会这样吗?

代码:

<?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

1 个答案:

答案 0 :(得分:1)

您的代码最终会首先处理$property[0],然后尝试使用其结果获取对象的属性。如果先得到$this->$property,那么你可以使用普通的数组符号来获取东西,并且事情会按预期工作。

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