0

I have a multidimensional object called $videos which contains a number of video objects.

I cast my $videos object to an array as follows

$videos = (array)$videos

When I var_dump($videos) it returns all of the data from $videos as shown below ($videos now being a multidimensional array)

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"

However if I try to var_dump any particular element of this array as shown below it returns null...

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

So why is var_dump() returning null when called with each individual element of the array??? Is it something to do with the visibility of the objects properties?

Anonymous
  • 11,748
  • 6
  • 35
  • 57
slickdrick
  • 29
  • 6

2 Answers2

1

The array doesn't have associated indices in it. It is stored by key, so you can access it by

var_dump($videos["query"]);

edit: If I see well, it's wordpress, So there's probably another way to do it with wordpress hooks

edit2: I see new formatting - try

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

I think it's what's you're going for.

Zbyszek Kisły
  • 2,110
  • 4
  • 26
  • 48
  • Thank you, I did not realize that one could not reference an assoc array by index (Which now seems painfully obvious!) – slickdrick Apr 20 '15 at 20:11
0

Basically, it is associative multidimensional array, means, you can access array elements by their specific index name instead of default index numbers.

In your case, you have main array $videos. which have two sub arrays such as, $videos["query"] and $videos["query_vars"]. Further, these two arrays consists of their respective arrays.

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

result: bf_video

Farjad Hasan
  • 3,354
  • 6
  • 23
  • 38
  • Thank you, I did not realize that one could not reference an assoc array by index (Which now seems painfully obvious!) – slickdrick Apr 20 '15 at 20:12