473,378 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

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
0 2467

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

Similar topics

3
by: Andy Fish | last post by:
Hi, I have a webpage with some javascript that constructs some data structures, some of which contain nested structures or arrays of other structures. It's the same order of complexity as a...
2
by: Pavils Jurjans | last post by:
Hello, I have a fairly complex project with server-side written in C# (.NET), and client-side heavily relying on the presence on JavaScript-compatible scripting engine. One of the features thie...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
7
by: Noozer | last post by:
I've been doing some reading about "AJAX" and it sounds pretty interesting. While I can make it work, it seem like I'm doing much of it the "hard way". I was wondering what kind of code that others...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
1
by: Red Daly | last post by:
Hello group, I have been using JSON for a while and it has made many things a breeze. However, JSON does not natively describe certain things like pointers and custom types. I created a simple...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
6
by: Jasper | last post by:
Hi, Maybe this is off-topic, but perhaps you can help. I'm looking for ideas on how to parse a data file. I dont know XML but I know it parses data in text format. I have a structured data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.