Results 1 to 6 of 6

Thread: Cannot Use Object of Type stdClass as Array

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Location
    Philadelphia, Pennsylvania
    Posts
    1,797

    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
    Join Date
    Jul 2009
    Location
    Dubuque, Iowa, USA
    Posts
    63
    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. #3
    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.

  4. #4
    Join Date
    Jan 2009
    Location
    Philadelphia, Pennsylvania
    Posts
    1,797
    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?

  5. #5
    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.



  6. #6
    Join Date
    Jul 2009
    Location
    India, Earth
    Posts
    26
    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, 20:13 PM
  3. How do get these type of links?
    By Sami4u in forum Promoting
    Replies: 5
    Last Post: 7 February, 2010, 03:42 AM
  4. [WTH] All type of SEO Services
    By MervikHaums in forum Marketplace
    Replies: 4
    Last Post: 25 November, 2009, 09: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
  •