Anthony Anthony - 5 months ago 11
JSON Question

Traversing and displaying JSON properties

How do I traverse and display the from_user and text properties from this page:


http://search.twitter.com/search.json?q=stackexchange


using this code?:

$json = json_decode(file_get_contents('http://search.twitter.com/search.json?q=stackexchange'));

foreach($json as $obj){
var_dump($obj);
}


Here is a var_dump() of this page:

float 0.021
float 3.2906566512425E+17
string '329065665124249601' (length=18)
string '?page=2&max_id=329065665124249601&q=stackexchange' (length=49)
int 1
string 'stackexchange' (length=13)
string '?since_id=329065665124249601&q=stackexchange' (length=44)
array (size=15)
0 =>
object(stdClass)[15]
public 'created_at' => string 'Tue, 30 Apr 2013 02:52:22 +0000' (length=31)
public 'from_user' => string '_MizMary' (length=8)
public 'from_user_id' => int 99464307
public 'from_user_id_str' => string '99464307' (length=8)
public 'from_user_name' => string '_Mary' (length=5)
public 'geo' => null
public 'id' => float 3.2906566512425E+17
public 'id_str' => string '329065665124249601' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[16]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/2413217134/b50_normal.gif' (length=60)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/2413217134/b50_normal.gif' (length=62)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'http://t.co/fLA534fKyW baking - Why didn't my beer cake cook properly? - Seasoned Advice: I have a beer cake re... http://t.co/tHK2E8CTEA' (length=137)
1 =>
object(stdClass)[17]
public 'created_at' => string 'Tue, 30 Apr 2013 02:52:22 +0000' (length=31)
public 'from_user' => string 'Abby_IBOk' (length=9)
public 'from_user_id' => int 577535154
public 'from_user_id_str' => string '577535154' (length=9)
public 'from_user_name' => string 'Abby Lane' (length=9)
public 'geo' => null
public 'id' => float 3.2906566297258E+17
public 'id_str' => string '329065662972579840' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[18]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/2259671740/faa073000166_normal.jpg' (length=69)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/2259671740/faa073000166_normal.jpg' (length=71)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'http://t.co/XbQmIDi6Mn baking - Why didn't my beer cake cook properly? - Seasoned Advice: I have a beer cake re... http://t.co/114vCTOHh2' (length=137)
2 =>
object(stdClass)[19]
public 'created_at' => string 'Tue, 30 Apr 2013 02:47:56 +0000' (length=31)
public 'from_user' => string 'PPUAMX' (length=6)
public 'from_user_id' => int 104957846
public 'from_user_id_str' => string '104957846' (length=9)
public 'from_user_name' => string 'PP-Biologia-UAMX' (length=16)
public 'geo' => null
public 'id' => float 3.290645475855E+17
public 'id_str' => string '329064547585499137' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[20]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/2518356894/082d12c6-9b55-4ff5-8c15-8227c5322114_normal.png' (length=93)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/2518356894/082d12c6-9b55-4ff5-8c15-8227c5322114_normal.png' (length=95)
public 'source' => string '<a href="http://twitter.com/#!/download/ipad">Twitter for iPad</a>' (length=88)
public 'text' => string 'RT @LaTeX_Chemistry: Range of a piano : annotate note frequency http://t.co/Zi7U5sNbLI' (length=86)
3 =>
object(stdClass)[21]
public 'created_at' => string 'Tue, 30 Apr 2013 02:45:47 +0000' (length=31)
public 'from_user' => string 'LaRassegna' (length=10)
public 'from_user_id' => int 119387853
public 'from_user_id_str' => string '119387853' (length=9)
public 'from_user_name' => string 'La Rassegna' (length=11)
public 'geo' => null
public 'id' => float 3.2906400498518E+17
public 'id_str' => string '329064004985176065' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[22]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/756451945/twitter_logo_2_normal.png' (length=70)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/756451945/twitter_logo_2_normal.png' (length=72)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'â–º Is this sentence grammatically Wrong? I have doubt. Please explain the answer in brief: The baby cried herse... http://t.co/Wmzthnpc4r' (length=138)
4 =>
object(stdClass)[23]
public 'created_at' => string 'Tue, 30 Apr 2013 02:45:46 +0000' (length=31)
public 'from_user' => string 'elithp' (length=6)
public 'from_user_id' => int 102653304
public 'from_user_id_str' => string '102653304' (length=9)
public 'from_user_name' => string 'EL' (length=2)
public 'geo' => null
public 'id' => float 3.2906400231341E+17
public 'id_str' => string '329064002313412608' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[24]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/sticky/default_profile_images/default_profile_5_normal.png' (length=78)
public 'profile_image_url_https' => string 'https://si0.twimg.com/sticky/default_profile_images/default_profile_5_normal.png' (length=80)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'Is this sentence grammatically Wrong? I have doubt. Please explain the answer in brief: The baby cried herself... http://t.co/OkN4yhVkpi' (length=136)
5 =>
object(stdClass)[25]
public 'created_at' => string 'Tue, 30 Apr 2013 02:38:56 +0000' (length=31)
public 'from_user' => string 'elithp' (length=6)
public 'from_user_id' => int 102653304
public 'from_user_id_str' => string '102653304' (length=9)
public 'from_user_name' => string 'EL' (length=2)
public 'geo' => null
public 'id' => float 3.290622824726E+17
public 'id_str' => string '329062282472603648' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[26]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/sticky/default_profile_images/default_profile_5_normal.png' (length=78)
public 'profile_image_url_https' => string 'https://si0.twimg.com/sticky/default_profile_images/default_profile_5_normal.png' (length=80)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'Why liquidity implies tight spread and low slippage: At this link: http://t.co/Hwfz6XJ0UB... http://t.co/9qW3MBukgR' (length=115)
6 =>
object(stdClass)[27]
public 'created_at' => string 'Tue, 30 Apr 2013 02:38:40 +0000' (length=31)
public 'from_user' => string 'JeromyAnglim' (length=12)
public 'from_user_id' => int 32311190
public 'from_user_id_str' => string '32311190' (length=8)
public 'from_user_name' => string 'Jeromy Anglim' (length=13)
public 'geo' => null
public 'id' => float 3.2906221650457E+17
public 'id_str' => string '329062216504573952' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[28]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1378429858/mugshot_normal.JPG' (length=64)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/1378429858/mugshot_normal.JPG' (length=66)
public 'source' => string '<a href="http://twitter.com/">web</a>' (length=59)
public 'text' => string 'Anyone able to answer my question about running JAGS+R simulations in the cloud http://t.co/qMIel5ZsMR #rstats #jags' (length=117)
7 =>
object(stdClass)[29]
public 'created_at' => string 'Tue, 30 Apr 2013 02:37:53 +0000' (length=31)
public 'from_user' => string 'vlizco' (length=6)
public 'from_user_id' => int 41031976
public 'from_user_id_str' => string '41031976' (length=8)
public 'from_user_name' => string 'Victor Lontoh' (length=13)
public 'geo' => null
public 'id' => float 3.2906201868442E+17
public 'id_str' => string '329062018684420097' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[30]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/923136180/Iron_Man_Teaser_normal.JPG' (length=71)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/923136180/Iron_Man_Teaser_normal.JPG' (length=73)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'WordPress – Set JPEG compression for specific custom image sizes - WordPress Answers: http://t.co/PCSgQl92oJ #php' (length=116)
8 =>
object(stdClass)[31]
public 'created_at' => string 'Tue, 30 Apr 2013 02:37:51 +0000' (length=31)
public 'from_user' => string 'StuckInActive' (length=13)
public 'from_user_id' => int 159326466
public 'from_user_id_str' => string '159326466' (length=9)
public 'from_user_name' => string 'John' (length=4)
public 'geo' => null
public 'id' => float 3.2906200816931E+17
public 'id_str' => string '329062008169308162' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[32]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1055646599/vd_lg_normal.jpg' (length=62)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/1055646599/vd_lg_normal.jpg' (length=64)
public 'source' => string '<a href="http://tapbots.com/tweetbot">Tweetbot for iOS</a>' (length=80)
public 'text' => string 'RT @brandonrbennett: Stack Exchange Q&A site proposal: Network Engineering http://t.co/Gd25B10f3e' (length=101)
9 =>
object(stdClass)[33]
public 'created_at' => string 'Tue, 30 Apr 2013 02:35:37 +0000' (length=31)
public 'from_user' => string 'statsqa' (length=7)
public 'from_user_id' => int 181885145
public 'from_user_id_str' => string '181885145' (length=9)
public 'from_user_name' => string 'CrossValidated' (length=14)
public 'geo' => null
public 'id' => float 3.2906144943349E+17
public 'id_str' => string '329061449433485314' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[34]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1165527255/xvalidated_normal.jpg' (length=67)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/1165527255/xvalidated_normal.jpg' (length=69)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'When doing regression analysis, when do I need to care about the regression method? http://t.co/YxNfX5uPaN' (length=106)
10 =>
object(stdClass)[35]
public 'created_at' => string 'Tue, 30 Apr 2013 02:34:02 +0000' (length=31)
public 'from_user' => string 'troyhunt' (length=8)
public 'from_user_id' => int 14414286
public 'from_user_id_str' => string '14414286' (length=8)
public 'from_user_name' => string 'Troy Hunt' (length=9)
public 'geo' => null
public 'id' => float 3.2906105151148E+17
public 'id_str' => string '329061051511476225' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[36]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1154092629/Square__small__normal.jpg' (length=71)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/1154092629/Square__small__normal.jpg' (length=73)
public 'source' => string '<a href="http://www.metrotwit.com/">MetroTwit</a>' (length=71)
public 'text' => string 'Interesting question by @Nick_Craver: How do I transition to SSL without affecting PageRank? http://t.co/fHmD9988Ez' (length=115)
11 =>
object(stdClass)[37]
public 'created_at' => string 'Tue, 30 Apr 2013 02:32:36 +0000' (length=31)
public 'from_user' => string 'BitcoinQuestion' (length=15)
public 'from_user_id' => int 366306993
public 'from_user_id_str' => string '366306993' (length=9)
public 'from_user_name' => string 'Bitcoin Q&A' (length=11)
public 'geo' => null
public 'id' => float 3.2906069047837E+17
public 'id_str' => string '329060690478374913' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[38]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1524282057/bitcoin530rotate_normal.png' (length=73)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/1524282057/bitcoin530rotate_normal.png' (length=75)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'Which function in which file verifies that transactions are the right size: Which file(s) are responsible for ... http://t.co/t8UR4gcI5c' (length=136)
12 =>
object(stdClass)[39]
public 'created_at' => string 'Tue, 30 Apr 2013 02:26:37 +0000' (length=31)
public 'from_user' => string 'fonnesbeck' (length=10)
public 'from_user_id' => int 14313383
public 'from_user_id_str' => string '14313383' (length=8)
public 'from_user_name' => string 'Chris Fonnesbeck' (length=16)
public 'geo' => null
public 'id' => float 3.2905918189209E+17
public 'id_str' => string '329059181892091904' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[40]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/1666776573/cohen_normal.png' (length=62)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/1666776573/cohen_normal.png' (length=64)
public 'source' => string '<a href="http://tapbots.com/software/tweetbot/mac">Tweetbot for Mac</a>' (length=93)
public 'text' => string 'I strongly encourage PyMC users to post questions to CrossValidated rather than to Google Groups. http://t.co/KfH7m1Cgme' (length=120)
13 =>
object(stdClass)[41]
public 'created_at' => string 'Tue, 30 Apr 2013 02:13:47 +0000' (length=31)
public 'from_user' => string 'DenubisX' (length=8)
public 'from_user_id' => int 14849850
public 'from_user_id_str' => string '14849850' (length=8)
public 'from_user_name' => string 'Brian BallsunStanton' (length=20)
public 'geo' => null
public 'id' => float 3.2905595168662E+17
public 'id_str' => string '329055951686619136' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[42]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/65309194/brian2_normal.jpg' (length=61)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/65309194/brian2_normal.jpg' (length=63)
public 'source' => string '<a href="http://twitter.com/">web</a>' (length=59)
public 'text' => string 'Why does everyone carry a 10' pole? http://t.co/BB3l5wSk3j' (length=58)
14 =>
object(stdClass)[43]
public 'created_at' => string 'Tue, 30 Apr 2013 02:10:13 +0000' (length=31)
public 'from_user' => string 'BitcoinOz' (length=9)
public 'from_user_id' => int 197776953
public 'from_user_id_str' => string '197776953' (length=9)
public 'from_user_name' => string 'Bitcoin' (length=7)
public 'geo' => null
public 'id' => float 3.2905505574554E+17
public 'id_str' => string '329055055745540096' (length=18)
public 'iso_language_code' => string 'en' (length=2)
public 'metadata' =>
object(stdClass)[44]
public 'result_type' => string 'recent' (length=6)
public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/2822646456/f4b390fafc8aed1d179e334880e2f9e4_normal.jpeg' (length=90)
public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/2822646456/f4b390fafc8aed1d179e334880e2f9e4_normal.jpeg' (length=92)
public 'source' => string '<a href="http://twitterfeed.com">twitterfeed</a>' (length=70)
public 'text' => string 'How is the probability of winning a block calculated from the difficulty?: I have problem finding thi... http://t.co/DNIszt4WLA #bitcoin' (length=136)
int 15
int 0
string '0' (length=1)

Answer

There is a collection of objects each containing from_user and text properties stored in an array called results. You can loop this array and get the properties like so:

    $json = json_decode(file_get_contents('http://search.twitter.com/search.json?q=stackexchange'));        
    foreach($json->results as $result){
       $from_user = $result->from_user;
       $text = $result->text;
       echo $from_user . ' ' .$text;
    }