Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing twitter trending output as an array in PHP

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!

like image 226
Jennifer Avatar asked Dec 07 '25 12:12

Jennifer


1 Answers

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;

    }

}
like image 121
Evert Avatar answered Dec 09 '25 02:12

Evert