Trying to access data inside Twitter json object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JonTwend
    New Member
    • Dec 2009
    • 1

    Trying to access data inside Twitter json object

    I am working with the Twitter API and finding it great fun and generally easy to use.

    But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the object data with a foreach loop and print it out. But I want to do more with the data than just print it.

    Just to be clear, I want to work with the daily and weekly trends but I am using the simpler current trends for this example. What I want to do is get the data out so that I can sort it, count duplicates, and remove duplicates.

    Here is my code:

    Code:
    $json = file_get_contents("http://search.twitter.com/trends/current.json",0,null,null);
    $json = json_decode($json);
    $data = array();
    foreach ( $json->trends as $trends )
    {
    	foreach ($trends as $val)
    		
    		echo "<a href='http://search.twitter.com/search?q=" . urlencode("{$val->query}") . "'>{$val->name}</a> &nbsp; ";
    		//array_push($data, "{$val->query}","{$val->name}");
    		
    		$data["name"] = "{$val->name}";
    		$data["query"] = "{$val->query}";
    }
    The echo statement works perfectly. The array in the assignment statements ends up with only the final two key-value pairs. I also tried an array_push (which is commented out) to assign the values, with the same results.

    The data I want from the json object is fairly deep. All I want to get at is the [name] and [query] values. Here is the output of a print_r for the original $json object:

    Code:
    stdClass Object
    (
        [trends] => stdClass Object
            (
                [2009-12-03 21:10:18] => Array
                    (
                        [0] => stdClass Object
                            (
                                [name] => #whoiam
                                [query] => #whoiam
                            )
    
                        [1] => stdClass Object
                            (
                                [name] => #thingsilove
                                [query] => #thingsilove
                            )
    
                        [2] => stdClass Object
                            (
                                [name] => Tiger Woods
                                [query] => "Tiger Woods"
                            )
    
                        [3] => stdClass Object
                            (
                                [name] => Pleasure P
                                [query] => "Pleasure P"
                            )
    
                        [4] => stdClass Object
                            (
                                [name] => Christmas
                                [query] => Christmas
                            )
    
                        [5] => stdClass Object
                            (
                                [name] => New Moon
                                [query] => "New Moon"
                            )
    
                        [6] => stdClass Object
                            (
                                [name] => #seenewmoonagain
                                [query] => #seenewmoonagain
                            )
    
                        [7] => stdClass Object
                            (
                                [name] => Grammy
                                [query] => Grammy
                            )
    
                        [8] => stdClass Object
                            (
                                [name] => Holiday
                                [query] => Holiday
                            )
    
                        [9] => stdClass Object
                            (
                                [name] => Santa
                                [query] => Santa
                            )
    
                    )
    
            )
    
        [as_of] => 1259874618
    )
    What am I missing here?
    Thanks for the help,
    Jon Meek
Working...