php - 将 Twitter 趋势输出解析为 PHP 中的数组

标签 php arrays parsing twitter hashtag

我正在尝试解析 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
                    )

            )

    )

)

我如何仅回应此输出中的两个趋势标签?我敢肯定这真的很简单,但我不是受过训练的程序员(这是针对心理学研究项目的)而且我有点迷路了。

非常感谢!

最佳答案

foreach($json_output[0]['trends'] as $trend) {

    if ($trend['name'][0] === '#') {

       echo $trend['name'];

    }

}

如果您最多想要 2 个趋势:

$count = 0;

foreach($json_output[0]['trends'] as $trend) {


    if ($trend['name'][0] === '#') {

       echo $trend['name'];
       $count++;

       if ($count === 2) break;

    }

}

关于php - 将 Twitter 趋势输出解析为 PHP 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953968/

相关文章:

php - 将 PHP 添加到外部 JAVASCRIPT 文件

javascript - 使用 react 按时间间隔更改图像

c++ - 无法创建动态整数数组

swift - 每天在 Swift 中执行一个特定的方法

linux - 在 Linux 中解析 SWF 文件

php - 查找第一组连续数字php正则表达式

php - 使用动态下拉列表从 MySQL 数据库加载文本区域中的数据

javascript - Twitter API 接受关注请求

php - 来自bind_result的数字打破了php数组

php - 简化 PHP DOM XML 解析——怎么做?