1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

How to Fix Cannot use object of type stdClass as array

Discussion in 'Content Management System' started by shabbir, Mar 13, 2011.

  1. shabbir

    shabbir Administrator Staff Member

    Working in Wordpress I wanted to check the type of page. I.e. is the content type as page or post. I used the global $post Wordpress variable and when I did variable dump for the object using print_r.
    echo '<pre>';print_r($post);echo'</pre>';
    I got
        [post_title] => Hello world!
        [post_excerpt] => Small Description about the post.
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] => 
        [post_name] => hello-world
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2011-03-12 13:57:23
        [post_modified_gmt] => 2011-03-12 13:57:23
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => Post URI
        [menu_order] => 0
        [post_type] => post
        [post_mime_type] => 
        [comment_count] => 0
        [ancestors] => Array
        [filter] => raw
    So when I tried using as $post['post_type'] I got the following error.

    Fatal error: Cannot use object of type stdClass as array

    Searching online I found lot of solutions that talk about Upgrading PHP as well as doing ini settings change. I was sure that I am not going to do anything of that sort and finally I found that $post is an object and not an array and so you should access it like

    and not

    I hope this helps lot of fellow Wordpress Developer's


    Assalamu alikum,
    Dear Brother,
    This is mohideen form chennai in tamilnadu.
    am having 2 dout .plz clear this problems
    1) how to create the single excel database....( plz tell me step wise)

    2)how to hack the another pc in command promt way ( plz tell me step wise)

    an trying to below way .but it is failier.

    first am typing net user then press enter key
    then am tracert gmail.com.pc showing some ip's. am choosing first ip
    then am doing ping that ip
    reply is success.
    i dont no after commond and process ...
    plz help me
  3. bhavanaets

    bhavanaets Banned

    when i echo it, it didn't show anything,
    echo $response;then i thought, maybe its an array so i put this code:
    echo "<pre>";
    echo "</pre>";it worked, i got these results on my browser: OUTPUT:
    object(stdClass)#6 (9) {
    string(20) "2007-07-18T04:50:52Z"
    string(7) "Success"
    string(13) "28c01821ba2c2"
    string(8) "2.400000"
    string(6) "1.0006"
    object(stdClass)#7 (2) {
    string(5) "40.00"
    string(3) "USD"
    string(1) "X"
    string(1) "M"
    string(17) "14E028078XJ827961UX"
    ok, i didn't want to show this to my users, instead, i wanted to show only the TransactionID part so i put this code instead:
    echo $response['TransactionID'][17];
    that's when i got this error:
    Fatal error: Cannot use object of type stdClass as array in C:windows-htdocs-cart.php on line 535
    ok, so how do you fix this error,i looked it up on google and found some post and forums where it say to upgrade my php or change the softwar or that i have a bad script. well, i finally figured out. it turns out that $response is actually an object. i have no idea what are objects in PHP but i came across an article about ojects and i found out that if you want to display it or show it with echo or print, you actually have to use the a different format. so in my example above, if i wanted to show the value TransactionID, this is how i would do it:

    echo $response->TransactionID;

Share This Page