By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Trying to access data inside Twitter json object

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. $json = file_get_contents("http://search.twitter.com/trends/current.json",0,null,null);
  2. $json = json_decode($json);
  3. $data = array();
  4. foreach ( $json->trends as $trends )
  5. {
  6.     foreach ($trends as $val)
  7.  
  8.         echo "<a href='http://search.twitter.com/search?q=" . urlencode("{$val->query}") . "'>{$val->name}</a> &nbsp; ";
  9.         //array_push($data, "{$val->query}","{$val->name}");
  10.  
  11.         $data["name"] = "{$val->name}";
  12.         $data["query"] = "{$val->query}";
  13. }
  14.  
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:

Expand|Select|Wrap|Line Numbers
  1. stdClass Object
  2. (
  3.     [trends] => stdClass Object
  4.         (
  5.             [2009-12-03 21:10:18] => Array
  6.                 (
  7.                     [0] => stdClass Object
  8.                         (
  9.                             [name] => #whoiam
  10.                             [query] => #whoiam
  11.                         )
  12.  
  13.                     [1] => stdClass Object
  14.                         (
  15.                             [name] => #thingsilove
  16.                             [query] => #thingsilove
  17.                         )
  18.  
  19.                     [2] => stdClass Object
  20.                         (
  21.                             [name] => Tiger Woods
  22.                             [query] => "Tiger Woods"
  23.                         )
  24.  
  25.                     [3] => stdClass Object
  26.                         (
  27.                             [name] => Pleasure P
  28.                             [query] => "Pleasure P"
  29.                         )
  30.  
  31.                     [4] => stdClass Object
  32.                         (
  33.                             [name] => Christmas
  34.                             [query] => Christmas
  35.                         )
  36.  
  37.                     [5] => stdClass Object
  38.                         (
  39.                             [name] => New Moon
  40.                             [query] => "New Moon"
  41.                         )
  42.  
  43.                     [6] => stdClass Object
  44.                         (
  45.                             [name] => #seenewmoonagain
  46.                             [query] => #seenewmoonagain
  47.                         )
  48.  
  49.                     [7] => stdClass Object
  50.                         (
  51.                             [name] => Grammy
  52.                             [query] => Grammy
  53.                         )
  54.  
  55.                     [8] => stdClass Object
  56.                         (
  57.                             [name] => Holiday
  58.                             [query] => Holiday
  59.                         )
  60.  
  61.                     [9] => stdClass Object
  62.                         (
  63.                             [name] => Santa
  64.                             [query] => Santa
  65.                         )
  66.  
  67.                 )
  68.  
  69.         )
  70.  
  71.     [as_of] => 1259874618
  72. )
  73.  
What am I missing here?
Thanks for the help,
Jon Meek
Dec 3 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.