Results 1 to 6 of 6

Thread: Cannot Use Object of Type stdClass as Array

  1. #1
    Kovich's Avatar
    Kovich is offline Community Guardian
    Join Date
    Jan 2009
    Location
    Philadelphia, Pennsylvania
    Posts
    1,797
    Blog Entries
    30
    Thanks
    453
    Thanked 420 Times in 279 Posts

    Cannot Use Object of Type stdClass as Array

    Fatal error: Cannot use object of type stdClass as array in **/index.php on line 18
    The code was working fine recently, but now, I suspect due to a PHP upgrade, is rendering the above referenced error message. I'm not really sure how to rectify something such as this, so any help would be greatly appreciated so I can get things running once again.

    Line 18 is within the below code, it begins with "$first_tweet."

    PHP Code:
    <?php
        
    if(isset($_POST['firstuser'])) {
            
    $first_user $_POST['firstuser'];
            
    $twitter = new Twitter($requestusername$requestpassword);
            
    $user1errorstatus false;
            
    $results false;
            
    $user_first_info json_decode($twitter->showUser("json"$first_user));
            if(isset(
    $user_first_info->error)) {
                
    $user1errorstatus true;
            }
            if(
    $user1errorstatus == false) {
                
    $num_tweets $user_first_info->statuses_count;
                
    $inpage ceil($num_tweets 20);
                
    $userstats $twitter->getUserTimeline($first_user$inpage);
                
    $userstats json_decode($userstats);
                
    $num_stats count($userstats) - 1;
                
    $first_tweet $userstats[$num_stats]->text;
                
    $results true;
            }
        }
    ?>

  2. #2
    Josh Manders's Avatar
    Josh Manders is offline Web Developer Extraordinaire
    Join Date
    Jul 2009
    Location
    Dubuque, Iowa, USA
    Posts
    63
    Thanks
    3
    Thanked 15 Times in 10 Posts
    Did you try dumping the array? No idea why a $_POST would have a object in it, but it could somehow have gotten one in it causing it to do that.

    It's probably trying to do like $_POST[0]->firstuser; That's what that error usually refers to.

    http://us4.php.net/manual/en/language.types.array.php

    And

    http://www.wallpaperama.com/forums/howto-fix-fatal-error-cannot-use-object-of-type-stdclass-as-array-in-php-t1735.html

    Both could be some use to help you fix the error.

  3. Thanked by:

    Kovich (22 June, 2010)

  4. #3
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    This error means that $userstats is not an array but an object so you have to use different notation to access it's elements i.e.
    $first_tweet = $userstats->$num_stats->text;

    Using print_r($userstats); and viewing the page source should reveal it's properties.

  5. Thanked by:

    Kovich (22 June, 2010)

  6. #4
    Kovich's Avatar
    Kovich is offline Community Guardian
    Join Date
    Jan 2009
    Location
    Philadelphia, Pennsylvania
    Posts
    1,797
    Blog Entries
    30
    Thanks
    453
    Thanked 420 Times in 279 Posts
    Alright, so I changed that line to:
    $first_tweet = $userstats->$num_stats->text;

    Now, the first tweet isn't showing. Was there something else I needed to change, or would this be the result of another issue entirely?

  7. #5
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,252
    Thanks
    173
    Thanked 303 Times in 230 Posts
    Now, the first tweet isn't showing. Was there something else I needed to change, or would this be the result of another issue entirely?
    I guess it is due to some other problem such as the $userstats not containing useful data.

    After the line
    $userstats = json_decode($userstats);

    $userstats may well contain an error message rather than an array of results.

    Following that line with print_r(
    $userstats); should reveal what it consists of.

    The print_r statement is very useful for this purpose.



  8. #6
    Naweed is offline Building the net, brick by brick
    Join Date
    Jul 2009
    Location
    India, Earth
    Posts
    26
    Thanks
    69
    Thanked 26 Times in 18 Posts
    It seems you aren't accessing the desired member variable correctly.

    Can you paste a part of the output you get for the print_r($userstats); statement?

    That'll have some interesting information.

Similar Threads

  1. Do you know your blood type?
    By Mike Dammann in forum General Chat
    Replies: 25
    Last Post: 8 April, 2014, 19:05 PM
  2. Where to put this type of thread?
    By Zash in forum Announcements and Suggestions
    Replies: 9
    Last Post: 11 February, 2010, 19:13 PM
  3. How do get these type of links?
    By Sami4u in forum Promoting
    Replies: 5
    Last Post: 7 February, 2010, 02:42 AM
  4. [WTH] All type of SEO Services
    By MervikHaums in forum Marketplace
    Replies: 4
    Last Post: 25 November, 2009, 08:20 AM
  5. Next best domain type
    By gavenecko in forum Domaining
    Replies: 15
    Last Post: 8 September, 2009, 04:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •