0

Using Twitter's search API, I'm being returned a helluva lot of information that is essentially extraneous to me. I'd like to populate an array with just the content of the tweets, none of the metadata.

Here's an example of a request and the response. So basically, I'd like to populate an array with the values of the text key.

I assume the answer would like in some kind of for loop however I don't know where to begin.

2 Answers 2

1

Something like this will do it:

<?
    $json = json_decode(file_get_contents('http://search.twitter.com/search.json?q=laughter&rpp=100&result_type=recent&lang=en'));
    $l = count($json->results);
    for ($i=0; $i < $l; $i++) { 
        echo $json->results[$i]->text . '<br/>';
    }
?>

Note: Use whatever reading method you need if file_get_contents isn't allowed to read remote.

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

Comments

0

try this:

Convert JSON to Array PHP

<?php
    //get json
    $foo= file_get_contents('http://search.twitter.com/search.json?q=stackoverflow&rpp=10&result_type=recent&lang=en');

    //convert to array
    $var = json_decode( $foo , true );

    //print array
    print_r( $var );
?>

Convert Array PHP to JSON

<?php
    //declarate array
    $foo = array( 1 ,2 ,3 ,4 ,5 , 6 => 7, 8);

    //convert to json
    $var = json_encode( $foo );

    //print json
    print_r( $var );
?>

Read:

As of PHP 5.2.0, the JSON extension is bundled and compiled into PHP by default.

See And Read This JSON PHP Documentation

AND

If you use PHP < 5.2, use JSON Class Objects

Bye!

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.