How can I create a new array in PHP using elements from a JSON return
Hello all having some trouble accessing elements from a returned JSON from Twitter. I'm trying to retrieve about 18 tweets from 6 different account and display them in chronological order, but I can't figure out how to access the elements in the array. I've tried using json_decode()
, tried foreach
loop and a for loop. Basically I out of all of the following, all I need is the following elements: text, user->screen_name, user->profile_image_url, and created_at. Reason being when I upload the php to a website, the app I'm building times out and doesn't receive the data, so I'm trying to make the return a lot smaller by only getting back what is needed. Anywho, here is a sample of 1 tweet I'm receiving back...
[{"in_reply_to_status_id":null,"created_at":"Thu Aug 25 01:50:07 +0000 2011","geo":null,"user":{"profile_background_tile":true,"protected":false,"url":"http:\/\/www.backpocketmemory.com","name":"Back Pocket Memory","default_profile":false,"friends_count":1115,"profile_link_color":"0084B4","default_profile_image":false,"show_all_inline_media":false,"contributors_enabled":false,"following":false,"geo_enabled":false,"utc_offset":-28800,"profile_sidebar_border_color":"C0DEED","description":"We're a hard rock\/pop band from Burbank, CA. We love you. facebook.com\/backpocketmemory @bpmchris @bpmguitar @bpmsixstring @bpmbass @bpmdrummer\r\n","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","time_zone":"Pacific Time (US & Canada)","created_at":"Mon Jul 27 20:58:35 +0000 2009","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","is_translator":false,"profile_use_background_image":true,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","follow_request_sent":false,"statuses_count":2238,"profile_background_color":"C0DEED","screen_name":"bckpcktmmry","profile_background开发者_StackOverflow中文版_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","favourites_count":6,"id_str":"60708421","lang":"en","verified":false,"profile_text_color":"333333","location":"Burbank, CA","id":60708421,"listed_count":19,"notifications":false,"profile_sidebar_fill_color":"DDEEF6","followers_count":683},"in_reply_to_user_id":null,"truncated":false,"coordinates":null,"favorited":false,"possibly_sensitive":false,"in_reply_to_status_id_str":null,"in_reply_to_screen_name":null,"source":"\u003Ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003EFacebook\u003C\/a\u003E","in_reply_to_user_id_str":null,"id_str":"106543849266098176","id":106543849266098176,"contributors":null,"place":null,"retweeted":false,"retweet_count":2,"text":"Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http:\/\/t.co\/oNvw5I6"}]
Any help is appreciated!
Your JSON decodes fine:
array (
0 =>
array (
'in_reply_to_status_id' => NULL,
'created_at' => 'Thu Aug 25 01:50:07 +0000 2011',
'geo' => NULL,
'user' =>
array (
'profile_background_tile' => true,
'protected' => false,
'url' => 'http://www.backpocketmemory.com',
'name' => 'Back Pocket Memory',
'default_profile' => false,
'friends_count' => 1115,
'profile_link_color' => '0084B4',
'default_profile_image' => false,
'show_all_inline_media' => false,
'contributors_enabled' => false,
'following' => false,
'geo_enabled' => false,
'utc_offset' => -28800,
'profile_sidebar_border_color' => 'C0DEED',
'description' => 'We're a hard rock/pop band from Burbank, CA. We love you. facebook.com/backpocketmemory @bpmchris @bpmguitar @bpmsixstring @bpmbass @bpmdrummer
',
'profile_background_image_url_https' => 'https://si0.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg',
'time_zone' => 'Pacific Time (US & Canada)',
'created_at' => 'Mon Jul 27 20:58:35 +0000 2009',
'profile_image_url' => 'http://a2.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg',
'is_translator' => false,
'profile_use_background_image' => true,
'profile_image_url_https' => 'https://si0.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg',
'follow_request_sent' => false,
'statuses_count' => 2238,
'profile_background_color' => 'C0DEED',
'screen_name' => 'bckpcktmmry',
'profile_background_image_url' => 'http://a3.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg',
'favourites_count' => 6,
'id_str' => '60708421',
'lang' => 'en',
'verified' => false,
'profile_text_color' => '333333',
'location' => 'Burbank, CA',
'id' => 60708421,
'listed_count' => 19,
'notifications' => false,
'profile_sidebar_fill_color' => 'DDEEF6',
'followers_count' => 683,
),
'in_reply_to_user_id' => NULL,
'truncated' => false,
'coordinates' => NULL,
'favorited' => false,
'possibly_sensitive' => false,
'in_reply_to_status_id_str' => NULL,
'in_reply_to_screen_name' => NULL,
'source' => '<a href="http://www.facebook.com/twitter" rel="nofollow">Facebook</a>',
'in_reply_to_user_id_str' => NULL,
'id_str' => '106543849266098176',
'id' => 106543849266098176,
'contributors' => NULL,
'place' => NULL,
'retweeted' => false,
'retweet_count' => 2,
'text' => 'Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http://t.co/oNvw5I6',
),
)
Echo all the user's avatars, names, and a date when they joined:
$data = json_decode($twitter_response);
foreach($data as $d)
{
echo '<img src="' . $d->user->profile_image_url . '" /> ' . $d->user->screen_name . ' (since ' . $d->user->created_at . ')<br />';
}
If you are using an associative array as your json_decode output, make sure you reference the appropriate keys, like so:
foreach($data as $d)
{
echo '<img src="' . $d['user']['profile_image_url'] . '" /> ' . $d['user']['screen_name'] . ' (since ' . $d['user']['created_at'] . ')<br />';
}
Working code: http://pastie.org/2436307 (removed single quotes in some values - just escape them or convert them when you decode/reference)
Use http://json.parser.online.fr/ to verify that the JSON you are trying to json_decode
is valid. That's the main function you need to use and it will return an array that you can use normally. Most likely your JSON is invalid and you'll need to get that cleaned up first.
There's no closing bracket, and thus it's not valid JSON data. Put ]
at the end of the string and it parses just fine with json_decode()
;
精彩评论