0

I have this code to tap into a twitter feed...

// 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;
  }

I want to access $text in another function. Can this be done?

5
  • Your question is about foreach. I don't see any foreach statement in you code. Commented Mar 26, 2013 at 20:45
  • Did you write this code ? Your request is simpler .... Commented Mar 26, 2013 at 20:46
  • Why not return an array of strings? Commented Mar 26, 2013 at 20:46
  • Access it in what way? Within the loop you can call a function and pass it the value. Or if the variable is in scope outside of the loop then code outside the loop can refer to either its pre-loop value or its post-loop value. What are you trying to accomplish? Commented Mar 26, 2013 at 20:46
  • Sorry. Working on multiple pieces of code and mistakenly put in foreach. I was trying to accomplish combining all tweets and going though the string to check for certain words. Commented Mar 26, 2013 at 21:03

1 Answer 1

1

just have an array and insert it on each run of the loop and then echo it out or return it.

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

 var_dump($arr);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.