1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
    Code:
    echo '<pre>';print_r($post);echo'</pre>';
    I got
    Code:
        [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

    PHP:
    $post->post_type
    and not

    PHP:
    $post['post_type']
    I hope this helps lot of fellow Wordpress Developer's
     
  2. MOHIDEEN THASTHAHIR

    MOHIDEEN THASTHAHIR New Member

    Joined:
    Feb 7, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Maersk
    Location:
    INIDA
    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

    Joined:
    Apr 8, 2011
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    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>";
    var_dump($response);
    echo "</pre>";it worked, i got these results on my browser: OUTPUT:
    object(stdClass)#6 (9) {
    ["Timestamp"]=>
    string(20) "2007-07-18T04:50:52Z"
    ["Ack"]=>
    string(7) "Success"
    ["CorrelationID"]=>
    string(13) "28c01821ba2c2"
    ["Version"]=>
    string(8) "2.400000"
    ["Build"]=>
    string(6) "1.0006"
    ["Amount"]=>
    object(stdClass)#7 (2) {
    ["_"]=>
    string(5) "40.00"
    ["currencyID"]=>
    string(3) "USD"
    }
    ["AVSCode"]=>
    string(1) "X"
    ["CVV2Code"]=>
    string(1) "M"
    ["TransactionID"]=>
    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:

    CODE
    echo $response->TransactionID;
    OUTPUT:
    14E028078XJ827961UX
     

Share This Page