从数组中回显数据

时间:2013-01-30 21:25:53

标签: php html twitter

我目前正在调用Twitter" Trending"使用PHP的API,我想返回前10个趋势主题。目前它似乎被回应到一个数组中,但我无法弄清楚如何获得一个单独的数据。

我想只为每个属性返回name属性。我怎么能这样做?

我现有的代码是:

<?

$search=file_get_contents("https://api.twitter.com/1/trends/1.json");
print_r(json_decode($search));

?>

解码数组:

Array
(
    [0] => stdClass Object
        (
            [trends] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => #EuroclubSoloEnEuropaFM
                            [url] => http://twitter.com/search?q=%23EuroclubSoloEnEuropaFM
                            [promoted_content] => 
                            [query] => %23EuroclubSoloEnEuropaFM
                            [events] => 
                        )

                    [1] => stdClass Object
                        (
                            [name] => #NBABands
                            [url] => http://twitter.com/search?q=%23NBABands
                            [promoted_content] => 
                            [query] => %23NBABands
                            [events] => 
                        )
                       //...

1 个答案:

答案 0 :(得分:2)

我认为json_decode会返回一个关联数组。您可以通过执行以下操作获取名称的值:

<?

$search=file_get_contents("https://api.twitter.com/1/trends/1.json");
$trends = json_decode($search);
foreach ($trends[0]->trends as $trend){
    echo $trend->name . "<br/>";
}
?>