Laravel 4,无法访问对象数组中的值

时间:2014-08-21 13:54:03

标签: php arrays laravel laravel-4

我使用Laravel 4.2和thujohn / twitter-l4访问twitter API。

我有这个代码,当我直接访问它时有效:

$timeline = Twitter::getUserTimeline(array('screen_name' => $username,
                'count' => 0,
                'include_rts' => false,
                'contributor_details' => false,
                'exclude_replies' => false
      ));

然后我可以dd($timeline),看到一个包含我需要的所有内容的数组:

    array (size=20)
  0 => 
    object(stdClass)[145]
      public 'created_at' => string 'Fri Aug 08 12:38:15 +0000 2014' (length=30)
      public 'id' => int 497723464770269184
      public 'id_str' => string '497723464770269184' (length=18)
      public 'text' => string '@crashplan I am trying to turn off 'Automatically renew subscriptions' on web interface. Won't turn off.' (length=104)
      public 'source' => string '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>' (length=66)
      public 'truncated' => boolean false
      public 'in_reply_to_status_id' => null
      public 'in_reply_to_status_id_str' => null
      public 'in_reply_to_user_id' => int 13753352
      public 'in_reply_to_user_id_str' => string '13753352' (length=8)
      public 'in_reply_to_screen_name' => string 'crashplan' (length=9)
      public 'user' => 
        object(stdClass)[146]
          public 'id' => int 14569524
          public 'id_str' => string '14569524' (length=8)
          public 'name' => string 'William Macdonald' (length=17)
          public 'screen_name' => string 'wmacdonald' (length=10)
          public 'location' => string 'Stockholm' (length=9)
          public 'description' => string 'William Macdonald Android/iPhone/PHP/Wordpress Developer and entrepreneur living in Stockholm.' (length=94)
          public 'url' => string 'http://t.co/lCnnv3wMmc' (length=22)
          public 'entities' => 
            object(stdClass)[147]
              ...
          public 'protected' => boolean false
          public 'followers_count' => int 164
          public 'friends_count' => int 253
          public 'listed_count' => int 11

然后我可以使用:$friends = $timeline[0]->user->friends_count;来获取我想要的数字。

然而,当我尝试使用jQuery来获得同样的结果时, Laravel 会在行$friends = $timeline[0]->user->friends_count处崩溃。

laravel.log中的错误消息,指向同一行代码:

  

不能使用stdClass类型的对象作为数组

我没有看到完全相同的代码如何以相同的方式产生不同的结果。

我正在使用MAMP&amp; Mac上的PHP 5.5.10。

0 个答案:

没有答案