Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Trying to display JSON object

    I am using the Twitter API and have the following JSON object returned after a search query. print_r($json) is:


    Code:
    $contents = file_get_contents("http://search.twitter.com/search.json?lang=en&rpp=5&q=MyNetwork");  
    $json = json_decode($contents);
    
    echo print_r($json);
    outputs:

    Code:
    stdClass Object ( [results] => Array ( [0] => stdClass Object ( [text] => Tune into Smackdown on MyNetwork right now & catch a Matt & Jeff Hardy heavy edition of the best wretling show on TV! Please Re-Tweet this! [to_user_id] => [from_user] => xxjillbabexx13 [id] => 3463574532 [from_user_id] => 32993629 [iso_language_code] => en [source] => <a href="http://twitter.com/">web</a> [profile_image_url] => http://a3.twimg.com/profile_images/367795893/jillian_09_009_normal.jpg [created_at] => Sat, 22 Aug 2009 01:53:19 +0000 ) [1] => stdClass Object ( [text] => Tune into Smackdown on MyNetwork right now & catch a Matt & Jeff Hardy heavy edition of the best wretling show on TV! Please Re-Tweet this! [to_user_id] => [from_user] => lizolee13 [id] => 3463565385 [from_user_id] => 19569029 [iso_language_code] => en [source] => <a href="http://www.atebits.com/" rel="nofollow">Tweetie</a> [profile_image_url] => http://s.twimg.com/a/1250203207/images/default_profile_normal.png [created_at] => Sat, 22 Aug 2009 01:52:48 +0000 ) [2] => stdClass Object ( [text] => Tune into Smackdown on MyNetwork right now & catch a Matt & Jeff Hardy heavy edition of the best wrestling show on TV! Please Re-Tweet this! [to_user_id] => [from_user] => 2xtreme4u [id] => 3463561766 [from_user_id] => 16055999 [iso_language_code] => en [source] => <a href="http://twitter.com/">web</a> [profile_image_url] => http://a3.twimg.com/profile_images/334787385/me_lookin_bad_normal.jpg [created_at] => Sat, 22 Aug 2009 01:52:36 +0000 ) [3] => stdClass Object ( [text] => RT @MATTHARDYBRAND: Tune into Smackdown on MyNetwork right now & catch a Matt & Jeff Hardy heavy edition of the best wretling show on TV! [to_user_id] => [from_user] => chelseajeann [id] => 3463351893 [from_user_id] => 31634310 [iso_language_code] => en [source] => <a href="http://twitterfon.net/" rel="nofollow">TwitterFon</a> [profile_image_url] => http://a3.twimg.com/profile_images/263229989/4155_82394743094_528598094_1790754_4065507_n_normal.jpg [created_at] => Sat, 22 Aug 2009 01:40:32 +0000 ) [4] => stdClass Object ( [text] => HardyShowsYuk: Tune into Smackdown on MyNetwork right now & catch a Matt & Jeff Hardy heavy edition of the best wretling show on TV! RT This [to_user_id] => [from_user] => esquire2385 [id] => 3463289960 [from_user_id] => 24079682 [iso_language_code] => en [source] => <a href="http://help.twitter.com/index.php?pg=kb.page&id=75" rel="nofollow">txt</a> [profile_image_url] => http://a1.twimg.com/profile_images/265742378/m_ade11887c36e4b7085a52c26f15f7a5c_normal.jpg [created_at] => Sat, 22 Aug 2009 01:36:57 +0000 ) ) [since_id] => 3319527376 [max_id] => 3463574532 [refresh_url] => ?since_id=3463574532&q=MyNetwork [results_per_page] => 5 [next_page] => ?page=2&max_id=3463574532&rpp=5〈=en&q=MyNetwork [warning] => adjusted since_id to 3319527376 (2009-08-15 01:00:00 UTC), requested since_id was older than allowed -- since_id removed for pagination. [completed_in] => 0.021453 [page] => 1 [query] => MyNetwork )

    so how do I display my JSON object using a for loop?

    Code:
    for( $counter = 0; $counter <= 5; $counter++){
         // what goes in here to display the "text" element from my JSON object?  i want to display "text" for all 5 responses returned from my query
    }

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    I have tried this:

    PHP Code:

    for( $counter 0$counter <= 5$counter++){
         echo 
    $json->$counter->text;

    When I use the above code I do not output anything. I get no errors either. The array is full of data because when I print_r($json) I get an array (I have pasted the results in the post above).

    Why is the array full of data elements but when I try to output each element in my for loop I get nothing in return?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •