2

I am new in php and am trying to read text and description from twitter api. My array looks like:

Array
(
    [0] => stdClass Object
        (
            [created_at] => Thu Oct 03 14:50:55 +0000 2013
            [id] => 3.85778998506E+17
            [id_str] => 385778998506033154
            [text] => We tested live-tweeting with @MLB to see if teams could boost follower engagement and their Twitter audience: https://t.co/XPhHmVDNxJ
            [source] => TweetDeck
            [truncated] => 
            [in_reply_to_status_id] => 
            [in_reply_to_status_id_str] => 
            [in_reply_to_user_id] => 
            [in_reply_to_user_id_str] => 
            [in_reply_to_screen_name] => 
            [user] => stdClass Object
                (
                    [id] => 783214
                    [id_str] => 783214
                    [name] => Twitter
                    [screen_name] => twitter
                    [location] => San Francisco, CA
                    [description] => Your official source for news, updates and tips from Twitter, Inc.
                    [url] => http://t.co/5iRhy7wTgu
                    [entities] => stdClass Object

I tried to read like this way but cant print anything echo $tweets->text;

1
  • 3
    If $tweets is an array, shouldn't it be $tweets[0]->text; Commented Oct 3, 2013 at 16:40

2 Answers 2

2

Try this:

echo $tweets[0]->text;
echo $tweets[0]->user->description;

I hope this will help you :)

Sign up to request clarification or add additional context in comments.

Comments

2

There is an outer array containing the object. Array access is done via brackets, so you can access the zeroth element via $tweets[0]. Now that you have the object, you can access its properties. All together this would be $tweets[0]->text. To get user values you would use $tweets[0]->user->description, for example.

If there were multiple tweets returned, you could use $tweets[1], etc. to access other tweet values. You can also iterate over these.

foreach ($tweets as $tweet) {
    echo $tweet->text;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.