ホームページ >バックエンド開発 >PHPチュートリアル >HTML で次の配列をトラバースする方法を教えてください。多くの方法を試しましたが、それでもエラーが発生します。
<code>Array ( [bigimg] => Array ( [0] => Array ( [id] => 2 [imgtitle] => abc [imgintro] => asdasd [filename] => sda.jpg [imgtype] => indeximg [created_at] => 2016-10-26 14:58:14 ) [1] => Array ( [id] => 1 [imgtitle] => sadas [imgintro] => asdasd [filename] => 147asda.jpg [imgtype] => indeximg [created_at] => 2016-10-24 14:13:12 ) ) [minimg] => Array ( ) )</code>
现在只想遍历数组bigimg
<code>Array ( [bigimg] => Array ( [0] => Array ( [id] => 2 [imgtitle] => abc [imgintro] => asdasd [filename] => sda.jpg [imgtype] => indeximg [created_at] => 2016-10-26 14:58:14 ) [1] => Array ( [id] => 1 [imgtitle] => sadas [imgintro] => asdasd [filename] => 147asda.jpg [imgtype] => indeximg [created_at] => 2016-10-24 14:13:12 ) ) [minimg] => Array ( ) )</code>
现在只想遍历数组bigimg
下面这样可符合你的要求?
<code class="javascript">var bigimg = [ [{id:2},{imgtitle:'abc'},{imgintro:'asdasd'},{filename:'sda.jpg'},{filename:'indeximg'},{created_at:'2016-10-26 14:58:14'}], [{id:1},{imgtitle:'sadas'},{imgintro:'asdasd'},{filename:'147asda.jpg'},{filename:'indeximg'},{created_at:'2016-10-24 14:13:12'}] ] var fn = function(arr){ for(var key in arr){ //如果子元素依然是数组或者Object则递归迭代 if(typeof arr[key] =='object'){ fn(arr[key]) }else{ console.log(key+'='+arr[key]) } } } fn(bigimg)</code>
已解决了! 在HTML 引用是 $bigimg[0][id]