鉴于您的示例 json 字符串,一个简单的 foreach 就足够了。考虑这个例子:
$json_string = '{"statuses":[{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Mon Jun 16 08:35:18 +0000 2014","id":478455763761786880,"id_str":"478455763761786880","text":"Later go shop at cold storage. LOLOL","source":"Twitter for Android<\/a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":559622523,"id_str":"559622523","name":"AhYong","screen_name":"likeasomebodeh","location":"To each and every road.","description":"Fun-Sized \/ CE1404N \/ BARVENGERS \/ Sagittarian \/ Sixth Gun \/ Drummer - Wannabe","url":"http:\/\/t.co\/PFSXy17655","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/PFSXy17655","expanded_url":"http:\/\/ask.fm\/Ah_Yong","display_url":"ask.fm\/Ah_Yong","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":329,"friends_count":444,"listed_count":1,"created_at":"Sat Apr 21 13:30:49 +0000 2012","favourites_count":4930,"utc_offset":28800,"time_zone":"Beijing","geo_enabled":false,"verified":false,"statuses_count":33116,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/459318345003593728\/mhjxKipm_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/459318345003593728\/mhjxKipm_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/559622523\/1401358655","profile_link_color":"009999","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"en"}],"search_metadata":{"completed_in":0.016,"max_id":478455763761786880,"max_id_str":"478455763761786880","next_results":"?max_id=478455763761786879&q=flu%20OR%20cold%20OR%20%23flu%20OR%20%23cold%20OR%20%23snot%20OR%20%23ill&count=1&include_entities=1","query":"flu+OR+cold+OR+%23flu+OR+%23cold+OR+%23snot+OR+%23ill","refresh_url":"?since_id=478455763761786880&q=flu%20OR%20cold%20OR%20%23flu%20OR%20%23cold%20OR%20%23snot%20OR%20%23ill&include_entities=1","count":1,"since_id":0,"since_id_str":"0"}}';
$json = json_decode($json_string, true);
foreach($json['statuses'] as $key => $json_values) {
if(isset($json_values['coordinates']) && !empty($json_values['coordinates'])) {
// continue your processes if key exists and not empty
}
}
重要提示:在您给定的 json 中,请注意,您的 coordinates 是 null