Parsing twitter trending output as an array in PHP -
Parsing twitter trending output as an array in PHP -
i'm trying parse twitter trending info isolate trending hashtags (not topics), , either echo them or save them variable. have next code:
$json_output=json_decode(file_get_contents("https://api.twitter.com/1/trends/23424977.json"),true); print_r($json_output);
which gives next 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] => 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 of america [woeid] => 23424977 ) ) ) )
how echo 2 trending hashtags output? i'm sure simple, i'm not trained programmer (this psychology research project) , i'm bit lost.
thank much!
foreach($json_output[0]['trends'] $trend) { if ($trend['name'][0] === '#') { echo $trend['name']; } }
if want 2 trends max:
$count = 0; foreach($json_output[0]['trends'] $trend) { if ($trend['name'][0] === '#') { echo $trend['name']; $count++; if ($count === 2) break; } }
php arrays parsing twitter hashtag
Comments
Post a Comment