2

私はPHPが初めてで、Twitter APIからテキスト説明を読み取ろうとしています。私の配列は次のようになります。

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

このように読み込もうとしましたが、何も印刷できませんecho $tweets->text;

4

2 に答える 2

2

これを試して:

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

これがお役に立てば幸いです:)

于 2013-10-03T16:48:51.703 に答える
2

オブジェクトを含む外部配列があります。配列へのアクセスは括弧を介して行われるため、 を介して 0 番目の要素にアクセスできます$tweets[0]。オブジェクトができたので、そのプロパティにアクセスできます。全体として、これは になります$tweets[0]->text。ユーザー値を取得する$tweets[0]->user->descriptionには、たとえば を使用します。

複数のツイートが返された場合は$tweets[1]、 などを使用して他のツイート値にアクセスできます。これらを反復することもできます。

foreach ($tweets as $tweet) {
    echo $tweet->text;
}
于 2013-10-03T16:41:33.337 に答える