2013-10-03 100 views
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;

+3

如果$鳴叫是一個數組,它不應該是$鳴叫[0] - >文本; – jjs9534

+0

謝謝了吧:) – lumos

回答

2

試試這個:

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

我希望這將幫助你:)

2

有包含對象的外部陣列。數組訪問通過括號完成,因此您可以通過$tweets[0]訪問第零個元素。現在您已擁有該對象,您可以訪問其屬性。一起這將是$tweets[0]->text。例如,要獲取用戶值,您可以使用$tweets[0]->user->description

如果有多個推文返回,您可以使用$tweets[1]等訪問其他推文值。你也可以迭代這些。

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