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


PHP foreach how to access variable outside loop

我有这个代码来进入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在另一个函数。这能做到吗?

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

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