2013-10-03 49 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; 
}