PHP foreach如何访问变量外部循环

时间:2013-03-26 20:44:23

标签: php twitter foreach

我有这个代码可以使用Twitter提要...

// Output tweets
  $json = file_get_contents("http://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=false&screen_name=*********&count=100", true);
  $decode = json_decode($json, true);

  $count = count($decode); //counting the number of status
  for($i=0;$i<$count;$i++){
    //echo $decode[$i]["text"]."<br><br>";
    $text = $decode[$i]["text"]." ";
    echo $text;
  }

我想在另一个函数中访问$ text。可以这样做吗?

1 个答案:

答案 0 :(得分:1)

只需要一个数组并在循环的每次运行中插入它,然后将其回显或返回它。

 $arr = array();
 for($i=0; $i<$count; $i++) {
   $text = $decode[$i]['text'];
   $arr[] = $text;
   echo $text . " ";
 }

 var_dump($arr);
相关问题