dingo_d
dingo_d

Reputation: 11670

Cannot access values of explicitly defined array in php

I am fetching an object from the database, and I 'converted' it to array so that I can use foreach on it.

$my_obj = (array) json_decode( get_option('my_options') );

This gets me array like this when I do a print_r on it:

Array (
    [0] => stdClass Object (
        [settings] => stdClass Object (
        [default] => 1 
        [header_title] => Separate title for this one! 
        [header_layout] => header_logo_centered 
        [fixed_header] => 1 
        [sticky_header] => 0 
        [transparent_header_transition] => 1 
        [select_menu] => centered-logo-header 
        [select_second_menu] => left-menu-header 
        [logo] => 
        [retina_logo] => ...wp-content/uploads/2015/12/sample.jpg 
        [retina_logo_width] => 
        [retina_logo_height] => 
        [transparent_logo] => 
        [transparent_retina_logo] => 
        [transparent_retina_logo_width] => 
        [transparent_retina_logo_height] => 
        [background_image] => ...wp-content/uploads/2015/08/audiothumb1.jpg 
        [background_color] => #848484 
        [text_color] => #397509 
        [text_hover_color] => 
        [transparent_text_color] => 
        [transparent_text_hover_color] => #146051 
        [test_select] => test_option_3 
        [test_textarea] => This is a test for textarea....ghghfsd 
        [test_pages_dropdown] => 5452 
        [icon_number] => 1 
        [test_icons_icon_0] => s7-magic-wand 
        [test_icons_value_0] => test2
        )
    )
    [1] => stdClass Object (
        [settings] => stdClass Object (
        [default] => 0 
        [header_title] => Test title here... 
        [header_layout] => header_layout_logo_left_magic_background 
        [fixed_header] => 
        [sticky_header] => 
        [transparent_header_transition] => 
        [select_menu] => 
        [select_second_menu] => 
        [logo] => 
        [retina_logo] => 
        [retina_logo_width] => 
        [retina_logo_height] => 
        [transparent_logo] => 
        [transparent_retina_logo] => 
        [transparent_retina_logo_width] => 
        [transparent_retina_logo_height] => 
        [background_image] => 
        [background_color] => 
        [text_color] => 
        [text_hover_color] => 
        [transparent_text_color] => 
        [transparent_text_hover_color] => 
        [test_select] => 
        [test_textarea] => 
        [test_pages_dropdown] => 
        [icon_number] => 0
        )
    )
)

Now when I try to do:

$my_obj[0]

I get

Notice undefined offset 0 in ....

And I cannot get anything out of it. But when I do a foreach on it, I can access my object just fine, and all its properties.

Why is this happening?

Upvotes: 0

Views: 141

Answers (2)

CatalinB
CatalinB

Reputation: 581

function objectToArray( $object ) {
    if( !is_object( $object ) && !is_array( $object ) ) {
        return $object;
    }
    if( is_object( $object ) ) {
        $object = get_object_vars( $object );
    }
    return array_map( 'objectToArray', $object );
}   

try this function ( for multidimensional obj ) ... and paste here the print_r

Upvotes: 0

Gandharv
Gandharv

Reputation: 110

  1. json_decode converts json to array so need to explicitly define array.
  2. For fetching an stdclass object you can pull data by $object->keyin your case $my_obj[0]->settings or you can convert object as array from following code

    foreach ($object as $value) 
      $array[] = $value->post_id;
    

Upvotes: 1

Related Questions