php var_dump为assoc数组的特定元素返回null,但不是整数数组

时间:2015-04-17 21:25:01

标签: php arrays wordpress multidimensional-array var-dump

我有一个名为$ videos的多维对象,其中包含许多视频对象。

我将$ videos对象转换为数组,如下所示

$videos = (array)$videos

当我var_dump($ videos)时,它会返回$ videos中的所有数据,如下所示($ videos现在是一个多维数组)

array(47) {
    ["query"] => array(3) {
        ["post_type"] => string(8) "bf_video"
        ["posts_per_page"] => int(10)
        ["paged"] => int(1)
    }
    ["query_vars"] => array(61) {
        ["post_type"] => string(8) "bf_video"
        ["posts_per_page"] => int(10)
        ["paged"] => int(1)
        ["error"] => string(0) ""
        ["m"] => string(0) ""
        ["p"] => int(0)
        ["post_parent"] => string(0) ""
        ["subpost"] => string(0) ""
        ["subpost_id"] => string(0) ""
        ["attachment"] => string(0) ""
        ["attachment_id"] => int(0)
        ["name"] => string(0) ""
        ["static"] => string(0) ""
        ["pagename"] => string(0) ""
        ["page_id"] => int(0)
        ["second"] => string(0) ""
        ["minute"] => string(0) ""
        ["hour"] => string(0) ""
        ["day"] => int(0)
        ["monthnum"] => int(0)
        ["year"] => int(0)
        ["w"] => int(0)
        ["category_name"] => string(0) ""
        ["tag"] => string(0) ""
        ["cat"] => string(0) ""
        ["tag_id"] => string(0) ""
        ["author"] => string(0) ""
        ["author_name"] => string(0) ""
        ["feed"] => string(0) ""
        ["tb"] => string(0) ""
        ["comments_popup"] => string(0) ""
        ["meta_key"] => string(0) ""
        ["meta_value"] => string(0) ""
        ["preview"] => string(0) ""
        ["s"] => string(0) ""
        ["sentence"] => string(0) ""
        ["fields"] => string(0) ""
        ["menu_order"] => string(0) ""
        ["category__in"] => array(0) {}
        ["category__not_in"] => array(0) {}
        ["category__and"] => array(0) {}
        ["post__in"] => array(0) {}
        ["post__not_in"] => array(0) {}
        ["tag__in"] => array(0) {}
        ["tag__not_in"] => array(0) {}
        ["tag__and"] => array(0) {}
        ["tag_slug__in"] => array(0) {}
        ["tag_slug__and"] => array(0) {}
        ["post_parent__in"] => array(0) {}
        ["post_parent__not_in"] => array(0) {}
        ["author__in"] => array(0) {}
        ["author__not_in"] => array(0) {}
        ["ignore_sticky_posts"] => bool(false)
        ["suppress_filters"] => bool(false)
        ["cache_results"] => bool(true)
        ["update_post_term_cache"] => bool(true)
        ["update_post_meta_cache"] => bool(true)
        ["nopaging"] => bool(false)
        ["comments_per_page"] => string(2) "50"
        ["no_found_rows"] => bool(false)
        ["order"] => string(4) "DESC"
    }
    ["tax_query"] => object(WP_Tax_Query)#859 (6) {
        ["queries"] => array(0) {}
        ["relation"] => string(3) "AND"
        ["table_aliases":protected] => array(0) {}
        ["queried_terms"] => array (0) {}
        ["primary_table"] => string(10) "live_posts"
        ["primary_id_column"] => string(2) "ID"
    }
    ["meta_query"] => object(WP_Meta_Query)#858 (7) {
        ["queries"] => array (0) {}
        ["relation"] => NULL
        ["meta_table"] => NULL
        ["meta_id_column"] => NULL
        ["primary_table"] => NULL
        ["primary_id_column"] => NULL
        ["table_aliases":protected] => array (0) {}
    }
    ["date_query"] => bool(false)
    ["request"] => string(234) "SELECT SQL_CALC_FOUND_ROWS live_posts.ID FROM live_posts WHERE 1=1 AND live_posts.post_type = 'bf_video' AND (live_posts.post_status = 'publish' OR live_posts.post_status = 'private') ORDER BY live_posts.post_date DESC LIMIT 0, 10"
    ["posts"] => array(10) {
        [0] => object(WP_Post)#851(24) {
            ["ID"] => int(505)
            ["post_author"] => string(1) "3"
            ["post_date"] => string(19) "2015-03-10 13:27:30"
            ["post_date_gmt"] => string(19) "2015-03-10 17:27:30"
            ["post_content"] => string(116) "carp on the fly in NY's croton watershed http://www.onehookup.blogspot.com http://www.jajphotography.wordpress.com"
            ["post_title"] => string(41) "Carp on the fly: Landing in a pot of gold"
            ["post_excerpt"] => string(0) ""
            ["post_status"] => string(7) "publish"

但是如果我尝试var_dump这个数组的任何特定元素,如下所示,它返回null ...

var_dump($videos[0]);
var_dump($videos[1]);
var_dump($videos[2]);

那么为什么var_dump()在使用数组的每个单独元素调用时返回null?它与对象属性的可见性有关吗?

2 个答案:

答案 0 :(得分:1)

数组中没有关联的索引。 它由密钥存储,因此您可以通过

访问它
var_dump($videos["query"]);

编辑: 如果我看得好,那就是wordpress,所以可能有另一种方法可以用wordpress钩子来实现它

EDIT2: 我看到了新的格式 - 尝试

var_dump($videos["posts"][0]);

我认为这就是你想要的。

答案 1 :(得分:0)

基本上,它是关联的多维数组,意思是,你可以通过它们的特定索引名而不是默认的索引号来访问数组元素。

在您的情况下,您有主阵列$视频。它有两个子数组,例如$ videos [“query”]和$ videos [“query_vars”]。此外,这两个数组由它们各自的数组组成。

echo $videos["query"]["post_type"];

结果:bf_video

相关问题