0

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?

7
  • Sounds like something else is going on in code you haven't shown. There is nothing wrong with what has been provided and it wouldn't give you that error. Commented Sep 15, 2016 at 8:34
  • Two possibilities: 0 isn't actually 0 but some character that looks like 0. Test that with print_r(array_map('bin2hex', array_keys($my_obj))). 0 should show up as 30. The other explanation is that you're doing something wrong. Commented Sep 15, 2016 at 8:35
  • stackoverflow.com/help/mcve : update the question with an example of json string. Commented Sep 15, 2016 at 8:52
  • 0 appears as 30. 0 Should be a number, right? I've tried print_r(array_map('gettype', array_keys($my_obj))); and I got Array ( [0] => string [1] => string ), could this be the issue? Commented Sep 15, 2016 at 8:52
  • 1
    $my_obj = (array) json_decode(...). What you probably want is json_decode(..., TRUE) Commented Sep 15, 2016 at 9:49

2 Answers 2

1
  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;
    
Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.