Home >Backend Development >PHP Tutorial >Please tell me how to traverse the following array in HTML. I have tried many methods and still get an error.

Please tell me how to traverse the following array in HTML. I have tried many methods and still get an error.

WBOY
WBOYOriginal
2016-12-01 01:27:23953browse

<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]

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn