Twitter API - 从 PHP 中的大型数组中提取一个值
Twitter API - Pulling a value from a large array in PHP
这是我从 Twitter API 调用返回的数组的第一部分。我正在尝试提取 [expanded_url] 变量并将其存储到另一个数组中。我让它适用于 [id] 或 [text] 等顶级项目,但我不确定如何访问更深层次的元素。
Array
(
[statuses] => Array
(
[0] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)
[created_at] => Thu Mar 26 21:02:42 +0000 2015
[id] => 581199619855650816
[id_str] => 581199619855650816
[text] => RT @JamilSays: 150 people recently died in a plane crash, ISIS is beheading innocent people, and you guys are bitching over Zayn leaving 1D…
[source] => Twitter for iPhone
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_status_id_str] =>
[in_reply_to_user_id] =>
[in_reply_to_user_id_str] =>
[in_reply_to_screen_name] =>
[user] => Array
(
[id] => 546187115
[id_str] => 546187115
[name] => Medina ✨
[screen_name] => Medinabtnc
[location] =>
[profile_location] =>
[description] => Bosnia and Herzegovina // Belgium // FC BAYERN MÜNCHEN ♡
[url] => http://t.co/z1LwFoDZ6U
[entities] => Array
(
[url] => Array
(
[urls] => Array
(
[0] => Array
(
[url] => http://t.co/z1LwFoDZ6U
[expanded_url] => http://izzzyyxo.tumblr.com
[display_url] => izzzyyxo.tumblr.com
[indices] => Array
(
[0] => 0
[1] => 22
)
)
)
)
[description] => Array
(
[urls] => Array
(
)
)
)
这是我用来访问和回显 [id] 的代码。我需要修改它以找到 [expanded_url]
foreach($string['statuses'] as $items)
{
echo $items['id']."<br />";
}
一旦我能够在循环中优化 [expanded_url],我想尽可能将每个 URL 存储到一个数组中!
这似乎在大多数情况下都有效,但有时它确实会导致错误。
foreach($string['statuses'] as $items)
{
if($items['user']['entities']['url']['urls']['0']['expanded_url'] !="")
echo $items['user']['entities']['url']['urls']['0']['expanded_url']."<br />";
这是我从 Twitter API 调用返回的数组的第一部分。我正在尝试提取 [expanded_url] 变量并将其存储到另一个数组中。我让它适用于 [id] 或 [text] 等顶级项目,但我不确定如何访问更深层次的元素。
Array
(
[statuses] => Array
(
[0] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)
[created_at] => Thu Mar 26 21:02:42 +0000 2015
[id] => 581199619855650816
[id_str] => 581199619855650816
[text] => RT @JamilSays: 150 people recently died in a plane crash, ISIS is beheading innocent people, and you guys are bitching over Zayn leaving 1D…
[source] => Twitter for iPhone
[truncated] =>
[in_reply_to_status_id] =>
[in_reply_to_status_id_str] =>
[in_reply_to_user_id] =>
[in_reply_to_user_id_str] =>
[in_reply_to_screen_name] =>
[user] => Array
(
[id] => 546187115
[id_str] => 546187115
[name] => Medina ✨
[screen_name] => Medinabtnc
[location] =>
[profile_location] =>
[description] => Bosnia and Herzegovina // Belgium // FC BAYERN MÜNCHEN ♡
[url] => http://t.co/z1LwFoDZ6U
[entities] => Array
(
[url] => Array
(
[urls] => Array
(
[0] => Array
(
[url] => http://t.co/z1LwFoDZ6U
[expanded_url] => http://izzzyyxo.tumblr.com
[display_url] => izzzyyxo.tumblr.com
[indices] => Array
(
[0] => 0
[1] => 22
)
)
)
)
[description] => Array
(
[urls] => Array
(
)
)
)
这是我用来访问和回显 [id] 的代码。我需要修改它以找到 [expanded_url]
foreach($string['statuses'] as $items)
{
echo $items['id']."<br />";
}
一旦我能够在循环中优化 [expanded_url],我想尽可能将每个 URL 存储到一个数组中!
这似乎在大多数情况下都有效,但有时它确实会导致错误。
foreach($string['statuses'] as $items)
{
if($items['user']['entities']['url']['urls']['0']['expanded_url'] !="")
echo $items['user']['entities']['url']['urls']['0']['expanded_url']."<br />";