-1

我目前正在使用 PHP 调用 Twitter 的“Trending”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] => 
                        )
                       //...
4

1 回答 1

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/>";
}
?>
于 2013-01-30T21:29:44.763 回答