json decode in php

Hello,

I have a sort of array printed from a JSON object (see below) and i would like to retrieve the values in [text], [sender_screen_name], [id] and [sender_id] from it, but do not understand how to use json_decode and retrieve the values from the results. Can someone help me do this in PHP?

Array ( [0] => stdClass Object ( [sender] => stdClass Object ( [id] => 31084145 [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png [friends_count] => 12 [default_profile_image] => 1 [location] => Dublin [show_all_inline_media] => [profile_background_color] => C0DEED [favourites_count] => 0 [follow_request_sent] => [following] => 1 [utc_offset] => [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png [name] => Swaran Singh [profile_link_color] => 0084B4 [protected] => [id_str] => 31084145 [default_profile] => 1 [profile_use_background_image] => 1 [profile_text_color] => 333333 [statuses_count] => 1 [description] => [notifications] => [profile_image_url] => http://a0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png [listed_count] => 0 [followers_count] => 9 [profile_sidebar_border_color] => C0DEED [url] => [screen_name] => chanianaus [verified] => [lang] => en [created_at] => Tue Apr 14 08:22:02 +0000 2009 [profile_background_tile] => [is_translator] => [contributors_enabled] => [profile_sidebar_fill_color] => DDEEF6 [time_zone] => [geo_enabled] => [profile_image_url_https] => https://si0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png ) [created_at] => Thu May 10 14:08:36 +0000 2012 [sender_id] => 31084145 [recipient] => stdClass Object ( [id] => 428575951 [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png [friends_count] => 18 [default_profile_image] => 1 [location] => [show_all_inline_media] => [profile_background_color] => C0DEED [favourites_count] => 0 [follow_request_sent] => [following] => [utc_offset] => [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png [name] => Diego Canale [profile_link_color] => 0084B4 [protected] => [id_str] => 428575951 [default_profile] => 1 [profile_use_background_image] => 1 [profile_text_color] => 333333 [statuses_count] => 11 [description] => [notifications] => [profile_image_url] => http://a0.twimg.com/sticky/default_profile_images/default_profile_6_normal.png [listed_count] => 0 [followers_count] => 3 [profile_sidebar_border_color] => C0DEED [url] => [screen_name] => Infobot2012 [verified] => [lang] => en [created_at] => Sun Dec 04 22:56:38 +0000 2011 [profile_background_tile] => [is_translator] => [contributors_enabled] => [profile_sidebar_fill_color] => DDEEF6 [time_zone] => [geo_enabled] => [profile_image_url_https] => https://si0.twimg.com/sticky/default_profile_images/default_profile_6_normal.png ) [sender_screen_name] => chanianaus [id_str] => 200588152493191170 [id] => 2.0058815249319E+17 [recipient_screen_name] => Infobot2012 [recipient_id] => 428575951 [text] => test ) )

This is how i got the above:


// create new instance
$OAuth = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);

$direct_message = $OAuth->get('https://api.twitter.com/1/direct_messages.json?count=1&page=');

print_r ($direct_message);

Well, usually, this is just an array. So, you can assign that output to an array.
$myData = new array();
$myData = your code to get that data…

Then, it is just an array which you should be able to access like $myData[‘text’]…

But, normally JSon is formatted a bit different using colon’s as separators. Then, you send it thru the PHP function json-decode to sort it back into an array format. It appears your data is already an array. So, just use it as an array… Here is a link that discusses it a bit. (Decoding Json)…
http://php.net/manual/en/function.json-decode.php
Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service