PHP避免在循环到数组的数组时使用double foreach [英] PHP avoid double foreach when looping into an array of an array
本文介绍了PHP避免在循环到数组的数组时使用double foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这种数组:
Array ( [data] => Array ( [0] => Array ( [name] => Lskdjlkdsfj [start_time] => 2013-06-12 [timezone] => Europe/Rome [location] => Rome, Lazio [id] => 592087844156650 [rsvp_status] => attending ) ) [paging] => Array ( [previous] => https://graph.facebook.com/100004042705860/events?limit=25&since=1370995200&__paging_token=592087844156650&__previous=1 [next] => https://graph.facebook.com/100004042705860/events?limit=25&until=1370995200&__paging_token=592087844156650 ) )
要获取我正在执行的数据:
To get the data I am doing so:
foreach ($fb_events as $data) {
foreach ($data as $fb_event_data) {
echo $fb_event_data['name'];
echo $fb_event_data['start_time'];
}
}
这是使用双foreach的最佳方法吗?有没有更好的方法可以循环到数组的数组中? 这个正在工作.我只是想知道我是否正在按照正确"的方式进行操作!
Is this the best way to do it, using a double foreach? Is there a better way to loop into an array of an array? This one is working. I am just wondering if I am doing it the "right" way!
推荐答案
如果顶级数组只有一个元素,则可以简化为
If the top level array only has a single element then this can be simplified as
foreach ($fb_events["data"] as $fb_event_data) {
echo $fb_event_data['name'];
echo $fb_event_data['start_time'];
}
这篇关于PHP避免在循环到数组的数组时使用double foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文