在 PHP 中将 Twitter 趋势输出解析为数组
我正在尝试解析 Twitter 趋势数据以仅隔离趋势标签(而不是主题),然后回显它们或将它们保存到变量中。目前我有以下代码:
$json_output=json_decode(file_get_contents("https://api.twitter.com/1/trends/23424977.json"),true);
print_r($json_output);
给出以下输出:
Array
(
[0] => Array
(
[as_of] => 2012-01-19T18:13:39Z
[trends] => Array
(
[0] => Array
(
[promoted_content] =>
[query] => %23youknowdamnwell
[name] => #youknowdamnwell
[events] =>
[url] => http://twitter.com/search/%23youknowdamnwell
)
[1] => Array
(
[query] => %23WhyGuysNeedPrenups
[name] => #WhyGuysNeedPrenups
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%23WhyGuysNeedPrenups
)
[2] => Array
(
[query] => TWUG
[url] => http://twitter.com/search/TWUG
[promoted_content] =>
[name] => TWUG
[events] =>
)
[3] => Array
(
[query] => %22The%20Bark%20Side%22
[name] => The Bark Side
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22The%20Bark%20Side%22
)
[4] => Array
(
[query] => %22Happy%20National%20Popcorn%20Day%22
[name] => Happy National Popcorn Day
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22Happy%20National%20Popcorn%20Day%22
)
[5] => Array
(
[query] => %22Marianne%20Gingrich%22
[name] => Marianne Gingrich
[events] =>
[url] => http://twitter.com/search/%22Marianne%20Gingrich%22
[promoted_content] =>
)
[6] => Array
(
[query] => %22Johnny%20Otis%22
[promoted_content] =>
[url] => http://twitter.com/search/%22Johnny%20Otis%22
[events] =>
[name] => Johnny Otis
)
[7] => Array
(
[query] => %22iBooks%202%22
[url] => http://twitter.com/search/%22iBooks%202%22
[promoted_content] =>
[events] =>
[name] => iBooks 2
)
[8] => Array
(
[query] => %22Heat%20vs%20Lakers%22
[name] => Heat vs Lakers
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22Heat%20vs%20Lakers%22
)
[9] => Array
(
[query] => %22Taylor%20Hall%22
[name] => Taylor Hall
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22Taylor%20Hall%22
)
)
[created_at] => 2012-01-19T18:09:12Z
[locations] => Array
(
[0] => Array
(
[name] => United States
[woeid] => 23424977
)
)
)
)
我如何从该输出中仅回显两个趋势标签?我确信这真的很简单,但我不是一个训练有素的程序员(这是一个心理学研究项目),我有点迷失了。
太感谢了!
I'm trying to parse twitter trending data to isolate just trending hashtags (not topics), and either echo them or save them to a variable. Currently I have the following code:
$json_output=json_decode(file_get_contents("https://api.twitter.com/1/trends/23424977.json"),true);
print_r($json_output);
Which gives the following output:
Array
(
[0] => Array
(
[as_of] => 2012-01-19T18:13:39Z
[trends] => Array
(
[0] => Array
(
[promoted_content] =>
[query] => %23youknowdamnwell
[name] => #youknowdamnwell
[events] =>
[url] => http://twitter.com/search/%23youknowdamnwell
)
[1] => Array
(
[query] => %23WhyGuysNeedPrenups
[name] => #WhyGuysNeedPrenups
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%23WhyGuysNeedPrenups
)
[2] => Array
(
[query] => TWUG
[url] => http://twitter.com/search/TWUG
[promoted_content] =>
[name] => TWUG
[events] =>
)
[3] => Array
(
[query] => %22The%20Bark%20Side%22
[name] => The Bark Side
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22The%20Bark%20Side%22
)
[4] => Array
(
[query] => %22Happy%20National%20Popcorn%20Day%22
[name] => Happy National Popcorn Day
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22Happy%20National%20Popcorn%20Day%22
)
[5] => Array
(
[query] => %22Marianne%20Gingrich%22
[name] => Marianne Gingrich
[events] =>
[url] => http://twitter.com/search/%22Marianne%20Gingrich%22
[promoted_content] =>
)
[6] => Array
(
[query] => %22Johnny%20Otis%22
[promoted_content] =>
[url] => http://twitter.com/search/%22Johnny%20Otis%22
[events] =>
[name] => Johnny Otis
)
[7] => Array
(
[query] => %22iBooks%202%22
[url] => http://twitter.com/search/%22iBooks%202%22
[promoted_content] =>
[events] =>
[name] => iBooks 2
)
[8] => Array
(
[query] => %22Heat%20vs%20Lakers%22
[name] => Heat vs Lakers
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22Heat%20vs%20Lakers%22
)
[9] => Array
(
[query] => %22Taylor%20Hall%22
[name] => Taylor Hall
[promoted_content] =>
[events] =>
[url] => http://twitter.com/search/%22Taylor%20Hall%22
)
)
[created_at] => 2012-01-19T18:09:12Z
[locations] => Array
(
[0] => Array
(
[name] => United States
[woeid] => 23424977
)
)
)
)
How would I echo just the two trending hashtags from this output? I'm sure this is really simple, but I'm not a trained programmer (this is for a psychology research project) and I'm a bit lost.
Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要最多 2 个趋势:
If you want 2 trends max: