如何使用JSON返回的元素在PHP中创建新数组

时间:2022-07-05 07:06:17

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...

您好,从Twitter返回的JSON访问元素时遇到一些问题。我试图从6个不同的帐户中检索大约18条推文并按时间顺序显示它们,但我无法弄清楚如何访问数组中的元素。我尝试过使用json_decode(),尝试使用foreach循环和for循环。基本上我是以下所有,我需要的是以下元素:text,user-> screen_name,user-> profile_image_url和created_at。原因是当我将php上传到网站时,我正在构建的应用程序超时并且没有收到数据,因此我试图通过仅返回所需内容来使返回更小。 Anywho,这是我收到的1条推文的样本......

[{"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_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"}]

[{“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“ :假的, “show_all_inline_media”:假的, “contributors_enabled”:假的, “以下”:假的, “geo_enabled”:假的, “utc_offset”: - 28800, “profile_sidebar_border_color”: “C0DEED”, “说明”:“我们来自加利福尼亚州伯班克的硬摇滚乐队。我们爱你.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”:假 “profile_use_background_image”:真实的, “profile_image_url_https”: “HTTPS:\ / \ / si0.twimg.com \ / profile_images \ / 1320311803 \ /STICKER_IDEA_1_normal.jpg”, “follow_request_sent”:假的, “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”,“verify”:false,“profile_text_color”:“333333”,“location”:“Burbank,CA”,“id”:60708421,“listed_count”: 19, “通知”:假的, “profile_sidebar_fill_color”: “DDEEF6”, “FOLLOWERS_COUNT”:683}, “in_reply_to_user_id”:空, “截断”:假的, “坐标”:空, “收藏”:假的, “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”, “标识”:10 6543849266098176,“contributors”:null,“place”:null,“转推”:false,“retweet_count”:2,“text”:“想在下周为Rob的Bday Show获取tix?排练前来参观我们。 156 Burbank的Providencia .... http:\ / \ / t.co \ / oNvw5I6“}]

Any help is appreciated!

任何帮助表示赞赏!

3 个解决方案

#1


1  

Your JSON decodes fine:

你的JSON解码很好:

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:

如果您使用关联数组作为json_decode输出,请确保引用相应的键,如下所示:

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)

工作代码:http://pastie.org/2436307(删除了某些值中的单引号 - 只需转义它们或在解码/引用时转换它们)

#2


0  

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.

使用http://json.parser.online.fr/验证您尝试json_decode的JSON是否有效。这是你需要使用的主要功能,它将返回一个你可以正常使用的数组。很可能你的JSON无效,你需要先清理它。

#3


0  

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();

没有结束括号,因此它不是有效的JSON数据。把它放在字符串的末尾,用json_decode()解析得很好;

#1


1  

Your JSON decodes fine:

你的JSON解码很好:

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:

如果您使用关联数组作为json_decode输出,请确保引用相应的键,如下所示:

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)

工作代码:http://pastie.org/2436307(删除了某些值中的单引号 - 只需转义它们或在解码/引用时转换它们)

#2


0  

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.

使用http://json.parser.online.fr/验证您尝试json_decode的JSON是否有效。这是你需要使用的主要功能,它将返回一个你可以正常使用的数组。很可能你的JSON无效,你需要先清理它。

#3


0  

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();

没有结束括号,因此它不是有效的JSON数据。把它放在字符串的末尾,用json_decode()解析得很好;