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

php var_dump returning null for specific elements of assoc array but not whole array

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

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

$videos = (array)$videos

当我 var_dump($videos) 它 returns 来自 $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 该数组的任何特定元素,如下所示 returns null...

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

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

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

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

编辑: 如果我没看错,那就是 wordpress,所以可能还有另一种方法可以使用 wordpress hooks

编辑2: 我看到了新格式 - 试试

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

我想这就是你想要的。

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

在您的例子中,您有主数组 $videos。它有两个子数组,例如 $videos["query"] 和 $videos["query_vars"]。更进一步,这两个数组由各自的数组组成。

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

结果:bf_video