首頁 >後端開發 >php教程 >請問下面這個陣列怎麼在HTML中遍歷出來,試了很多方法還是報錯

請問下面這個陣列怎麼在HTML中遍歷出來,試了很多方法還是報錯

WBOY
WBOY原創
2016-12-01 01:27:23950瀏覽

<code>Array
(
    [bigimg] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [id] =&gt; 2
                    [imgtitle] =&gt; abc
                    [imgintro] =&gt; asdasd
                    [filename] =&gt; sda.jpg
                    [imgtype] =&gt; indeximg
                    [created_at] =&gt; 2016-10-26 14:58:14
                )

            [1] =&gt; Array
                (
                    [id] =&gt; 1
                    [imgtitle] =&gt; sadas
                    [imgintro] =&gt; asdasd
                    [filename] =&gt; 147asda.jpg
                    [imgtype] =&gt; indeximg
                    [created_at] =&gt; 2016-10-24 14:13:12
                )

        )

    [minimg] =&gt; Array
        (
        )

)</code>

现在只想遍历数组bigimg

回复内容:

<code>Array
(
    [bigimg] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [id] =&gt; 2
                    [imgtitle] =&gt; abc
                    [imgintro] =&gt; asdasd
                    [filename] =&gt; sda.jpg
                    [imgtype] =&gt; indeximg
                    [created_at] =&gt; 2016-10-26 14:58:14
                )

            [1] =&gt; Array
                (
                    [id] =&gt; 1
                    [imgtitle] =&gt; sadas
                    [imgintro] =&gt; asdasd
                    [filename] =&gt; 147asda.jpg
                    [imgtype] =&gt; indeximg
                    [created_at] =&gt; 2016-10-24 14:13:12
                )

        )

    [minimg] =&gt; 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]

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn