3

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!

1 Answer 1

2
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;

    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

Ah, I think I wasn't particularly clear. I'm trying to create a loop that would go through whatever array is collected (after multiple calls) and check each ['name'] to see if it starts with a hashtag and then, if it does, echo it/save it to a variable. Does that make sense?
You are officially my hero. :D
Does this title come with a prize??
My undying gratitude and...COOKIES. Well, at least one of the two. ;)
Haha, awesome. Good luck with your project
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.