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?
0isn't actually0but some character that looks like0. Test that withprint_r(array_map('bin2hex', array_keys($my_obj))).0should show up as30. The other explanation is that you're doing something wrong.print_r(array_map('gettype', array_keys($my_obj)));and I gotArray ( [0] => string [1] => string ), could this be the issue?$my_obj = (array) json_decode(...). What you probably want isjson_decode(..., TRUE)