我目前正在调用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] =>
)
//...
答案 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/>";
}
?>