Lost in array!

Discussion in 'PHP' started by Sleeping Troll, Jun 6, 2009.

  1. #1
    I am retrieving a twitter feed in json, I am converting it to a php array, but I can't seem to succesfully pull vals from it.
    <?php
    $ch = curl_init();
    
    curl_setopt ($ch, CURLOPT_URL, "http://twitter.com/statuses/public_timeline.json");
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $twitter = curl_exec($ch);
    curl_close($ch);
    $twitterArray = json_decode($twitter);
    array_walk_recursive($twitterArray, 'Assign');
    function Assign(){
    	$image=$twitterArray["user"]["profile_image_url"];
    	$name=$twitterArray["user"]["screen_name"];
    	$description=$twitterArray["user"]["description"];
    	$created=$twitterArray["user"]["created_at"];
    	$followers=$twitterArray["user"]["followers_count"];
    	$friends=$twitterArray["user"]["friends_count"];
    	print_r("
    		$image\n
    		$name\n
    		$description\n
    		$created\n
    		$followers\n
    		$friends\n
    	");
    }
    ?>
    PHP:
    here is a sample of the array:

    Array
    (
        [0] => Array
            (
                [truncated] => 
                [created_at] => Sat Jun 06 15:07:27 +0000 2009
                [user] => Array
                    (
                        [time_zone] => Eastern Time (US & Canada)
                        [profile_link_color] => D02B55
                        [description] => beepbeep.. I mean tweet tweet?
                        [profile_background_tile] => 
                        [utc_offset] => -18000
                        [created_at] => Wed Feb 18 22:35:17 +0000 2009
                        [profile_image_url] => http://s3.amazonaws.com/twitter_production/profile_images/80258680/Picture_10_normal.png
                        [profile_background_color] => 352726
                        [screen_name] => victoriacheng
                        [statuses_count] => 43
                        [profile_sidebar_fill_color] => 99CC33
                        [url] => http://xanga.com/tofunator
                        [name] => Victoria Cheng
                        [following] => 
                        [favourites_count] => 0
                        [protected] => 
                        [profile_sidebar_border_color] => 829D5E
                        [followers_count] => 44
                        [notifications] => 
                        [profile_text_color] => 3E4415
                        [location] => New York
                        [id] => 21249875
                        [profile_background_image_url] => http://static.twitter.com/images/themes/theme5/bg.gif
                        [friends_count] => 37
                    )
    
                [in_reply_to_status_id] => 
                [in_reply_to_user_id] => 
                [text] => I'm glad I didn't post last night.  Drunk posting can be more damaging than drunk calls sometimes.
                [favorited] => 
                [in_reply_to_screen_name] => 
                [id] => 2054808134
                [source] => web
            )
    
        [1] => Array
            (
                [truncated] => 
                [created_at] => Sat Jun 06 15:07:27 +0000 2009
                [user] => Array
                    (
                        [time_zone] => Central Time (US & Canada)
                        [profile_link_color] => 1a0699
                        [description] => Attractive Ebony enjoys exploiting, cuckolding, heel grinding and black mailing slaves. What's your fetish?
                        [profile_background_tile] => 1
                        [utc_offset] => -21600
                        [created_at] => Thu Apr 09 13:15:50 +0000 2009
                        [profile_image_url] => http://s3.amazonaws.com/twitter_production/profile_images/130502393/yellow_arch_normal.jpg
                        [profile_background_color] => 1fcb15
                        [screen_name] => EbonySeductress
                        [statuses_count] => 77
                        [profile_sidebar_fill_color] => c1eaaa
                        [url] => http://www.niteflirt.com/Empress of Seduction
                        [name] => Ebony Dominatrix
                        [following] => 
                        [favourites_count] => 13
                        [protected] => 
                        [profile_sidebar_border_color] => 85d97e
                        [followers_count] => 383
                        [notifica...
    Code (markup):
    Please help, my brain hurts! Link to results: http://orbzorbz.com/php/twitter.php
     
    Sleeping Troll, Jun 6, 2009 IP
  2. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $ch = curl_init();
    
    curl_setopt ($ch, CURLOPT_URL, "http://twitter.com/statuses/public_timeline.json");
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $twitter = curl_exec($ch);
    curl_close($ch);
    $twitterArray = json_decode($twitter);
    
    foreach ($twitterArray as $twat) {
      echo $twat->user->description . '<br />';
      echo $twat->user->created_at . '<br />';
      echo $twat->user->screen_name . '<br />';
    }
    PHP:
     
    JDevereux, Jun 6, 2009 IP