0

Can someone tell me how I can loop through the below array?

http://pastebin.com/rhaF5Zdi

I've tried with out luck:

$_data = json_decode($_data);
foreach ( $_data as $tweet )
{
    echo "{$tweet->text}\n";
}

thanks

ps: im follwoing this php script. http://mikepultz.com/2013/06/mining-twitter-api-v1-1-streams-from-php-with-oauth/

hers another paste bin on the array. there seems to be multiple arrays happening http://pastebin.com/dduzhpqY

4
  • Where is it coming from? Something went wrong decoding it, see those => everywhere? That doesn't look right... What errors are you getting? Commented Sep 26, 2013 at 0:08
  • Ah sorry my bad, copy and paste did something weird. I've updated the array data. thanks Commented Sep 26, 2013 at 0:17
  • Have you tried - echo $tweet->text . '\n'; ? Commented Sep 26, 2013 at 0:18
  • Just tried and it doesn't seem to work. If I print that I get the error message "Trying to get property of non-object" so i guessing its not an object but an array? so I tried $_data['text'] Commented Sep 26, 2013 at 0:37

3 Answers 3

1

It looks like you might be creating a PHP stnd object instead of an array

//this will create a php standard object
$objOfData=json_decode($json); 

Instead Use the version below: (Notice the the 2nd parameter is TRUE)

$associativeArray=json_decode($json, TRUE); 

This will turn the object into an associative array and you can access fields like so:

$id=$associativeArray['id'];

More info here: json_decode

Sign up to request clarification or add additional context in comments.

1 Comment

I think i'm doing in correctly but something must be wrong. ' $data = json_decode($json, TRUE); if ($data) { // process it $this->process_tweet($data); }' then ' private function process_tweet($_data) { echo $_data['text']; }'
0

The code in posted link is not JSON, but it is output of print_r() function. PHP has no invert function to print_r(), but on php.net in print_r() documentation's comments you can find some user-made functions which can read it.

For example this one: http://www.php.net/manual/en/function.print-r.php#93529

I hope it will help. Good Luck :)

Comments

0

Ended up using:

if(array_key_exists('text', $_data)){
        echo 'Tweet ID = '.$_data['id_str'];
        echo 'Tweet Text = '.$_data['text'];
        echo '<br /><br />';
    }

cheers

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.