Foreach循环通过PHP中的json_decode数组

时间:2014-08-04 21:35:18

标签: php json twitter foreach

我试图通过数组循环,但遇到了麻烦,我得到了“Undefined index:text”

这是代码:

  

$ tweets = json_decode($ response,true);

     

foreach($ tweets为$ tweet){

   echo $tweet['text'];
     

}

JSON数据如下所示:

  

对象(stdClass的)[2]

 public 'statuses' => 

  array (size=15)

  0 => 

   object(stdClass)[3]

     public 'metadata' => 

       object(stdClass)[4]

         ...

     public 'created_at' => string 'Mon Aug 04 21:25:41 +0000 2014' (length=30)

     public 'id' => float 4.9640664406701E+17

     public 'id_str' => string '496406644067012608' (length=18)

     public 'text' => string 'Youth is served (in big helpings): 14 Chiefs could >feature NFLs youngest OL..  #Chiefs #NFL' (length=91)

2 个答案:

答案 0 :(得分:0)

当你需要遍历状态时,你正在循环外部对象:

 foreach ($tweets['statuses'] as $tweet) {

答案 1 :(得分:0)

你有两个问题'状态'是数组,所以json_decode($response->statuses,true);和text是对象的属性,所以echo $tweet->text;