我有从Array中的Twitter API调用返回的数据数组。我正在重复"十大趋势"但是不能打印单个元素,例如名称。下面是我尽可能使用的代码:
echo "<pre>";
print_r($string);
echo "</pre>";
结果如下:
Array
(
[trends] => Array
(
[0] => Array
(
[name] => #PagiYlanissa
[query] => %23PagiYlanissa
[url] => http://twitter.com/search?q=%23PagiYlanissa
[promoted_content] =>
)
[1] => Array
(
[name] => Torikul SMKN2 SRAGEN
[query] => %22Torikul+SMKN2+SRAGEN%22
[url] => http://twitter.com/search?q=%22Torikul+SMKN2+SRAGEN%22
[promoted_content] =>
)
[2] => Array
(
[name] => Wali Supit Tamfan Dan Berani
[query] => %22Wali+Supit+Tamfan+Dan+Berani%22
[url] => http://twitter.com/search?q=%22Wali+Supit+Tamfan+Dan+Berani%22
[promoted_content] =>
)
[3] => Array
(
[name] => #SelamatMalamNadyaFatira
[query] => %23SelamatMalamNadyaFatira
[url] => http://twitter.com/search?q=%23SelamatMalamNadyaFatira
[promoted_content] =>
)
[4] => Array
(
[name] => #fireproof
[query] => %23fireproof
[url] => http://twitter.com/search?q=%23fireproof
[promoted_content] =>
)
[5] => Array
(
[name] => Bersyukur
[query] => Bersyukur
[url] => http://twitter.com/search?q=Bersyukur
[promoted_content] =>
)
[6] => Array
(
[name] => Selamat Hari Olahraga Nasional
[query] => %22Selamat+Hari+Olahraga+Nasional%22
[url] => http://twitter.com/search?q=%22Selamat+Hari+Olahraga+Nasional%22
[promoted_content] =>
)
[7] => Array
(
[name] => #RIPRise
[query] => %23RIPRise
[url] => http://twitter.com/search?q=%23RIPRise
[promoted_content] =>
)
[8] => Array
(
[name] => Tanggal 9
[query] => %22Tanggal+9%22
[url] => http://twitter.com/search?q=%22Tanggal+9%22
[promoted_content] =>
)
[9] => Array
(
[name] => #LP3IPastiKerja
[query] => %23LP3IPastiKerja
[url] => http://twitter.com/search?q=%23LP3IPastiKerja
[promoted_content] =>
)
)
[as_of] => 2014-09-08T23:30:17Z
[created_at] => 2014-09-08T23:25:24Z
[locations] => Array
(
[0] => Array
(
[name] => Indonesia
[woeid] => 23424846
)
)
)
但是我无法通过顶部的Array访问各个元素。有什么建议吗?
答案 0 :(得分:1)
这应该有效:
$trends = $string[0]['trends'];
foreach ($trends as $trend) {
// Do stuff with $trend['name'], $trend['query'], etc.
}
答案 1 :(得分:0)
类似的东西:
foreach ($string[0]['trends'] as $trending)
{
extract($trending);
echo $name.'<br>';
}