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

发布于 2024-12-28 08:40:26 字数 4393 浏览 1 评论 0原文

我正在尝试解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2025-01-04 08:40:26
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;

    }

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

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

       echo $trend['name'];

    }

}

If you want 2 trends max:

$count = 0;

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


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

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

       if ($count === 2) break;

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文