在PHP中将Twitter趋势输出解析为数组

3

我正在尝试解析Twitter趋势数据,只隔离流行的hashtags(不是话题),并将它们回显或保存到变量中。目前我有以下代码:

$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
                    )

            )

    )

)

如何从这个输出中仅打印出两个热门标签?我确定这非常简单,但是我不是一名受过训练的程序员(这是为心理学研究项目而做的)我有点迷失了。

非常感谢!

1个回答

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

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

       echo $trend['name'];

    }

}

如果你只想要两种趋势:

$count = 0;

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


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

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

       if ($count === 2) break;

    }

}

啊,我觉得我表达得不是特别清楚。我正在尝试创建一个循环,它会遍历收集到的任何数组(在多次调用后),并检查每个 ['name'] 是否以井号开头,如果是,则将其输出/保存到一个变量中。这样说清楚了吗? - Jennifer
你正式成为我的英雄。 :D - Jennifer
这个标题有奖励吗? - Evert
我的无尽感激和...饼干。好吧,至少其中之一。 ;) - Jennifer
哈哈,太棒了。祝你的项目好运。 - Evert
如何在推文不为空的情况下获取tweet_volume? [name] => Mステ [url] => twitter.com/search?q=M%E3%82%B9%E3%83%86 [promoted_content] => [query] => M%E3%82%B9%E3%83%86 [tweet_volume] => 102516 某些趋势中,Tweet volum为空... - Srinivas08

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接