php - 遍历索引数组

标签 php html arrays foreach

Array
(
    [2] => Array
        (
            [12] => Array
                (
                    [0] => Array
                        (
                            [id] => 24339
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118953&hash=545584bd3253fab9&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 24483
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809310&hash=5544f9e52b6ebc10&hd=1
                        )

                )

            [11] => Array
                (
                    [0] => Array
                        (
                            [id] => 24016
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118860&hash=088323c81fb1f360&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 24115
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809379&hash=dd49270049f5a632&hd=1
                        )

                )

            [10] => Array
                (
                    [0] => Array
                        (
                            [id] => 23463
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118763&hash=f328401f5bd7f2e4&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 23656
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809462&hash=7a61a9ac444e2b95&hd=1
                        )

                )

            [9] => Array
                (
                    [0] => Array
                        (
                            [id] => 21815
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118684&hash=3bbddfcc12426510&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 22406
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809548&hash=cd0e7f43171569a6&hd=1
                        )

                )

            [8] => Array
                (
                    [0] => Array
                        (
                            [id] => 20443
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118608&hash=26c0f730bc1dd6c3&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 20921
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809649&hash=47c524f47946fb96&hd=1
                        )

                )

            [7] => Array
                (
                    [0] => Array
                        (
                            [id] => 19812
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118510&hash=1243ca95114cbdf2&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 20057
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809741&hash=266c635751d2f9d5&hd=1
                        )

                )

            [6] => Array
                (
                    [0] => Array
                        (
                            [id] => 19262
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118374&hash=e947746533f9d895&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 19392
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809836&hash=e58901e3e2f32993&hd=1
                        )

                )

            [5] => Array
                (
                    [0] => Array
                        (
                            [id] => 18217
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118222&hash=3a236df89bcfaac5&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 18417
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164809927&hash=d8a72be4d4eb7d86&hd=1
                        )

                )

            [4] => Array
                (
                    [0] => Array
                        (
                            [id] => 17231
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165118092&hash=13e854824bde17ab&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 17603
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164810038&hash=9d7b6a4336dd2b98&hd=1
                        )

                )

            [3] => Array
                (
                    [0] => Array
                        (
                            [id] => 15838
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165117998&hash=031a9c6d70058e4f&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 16465
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164810119&hash=06985923604c61ca&hd=1
                        )

                )

            [2] => Array
                (
                    [0] => Array
                        (
                            [id] => 15070
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165117903&hash=b1a21a9021b74aa2&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 15284
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164810199&hash=3b818c6ef7c7488b&hd=1
                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 13663
                            [active] => 1
                            [lang] => en
                            [link] => http://vk.com/video_ext.php?oid=203859683&id=165117810&hash=62f01fa6c53a195f&hd=1
                        )

                    [1] => Array
                        (
                            [id] => 14000
                            [active] => 1
                            [lang] => ru
                            [link] => http://vk.com/video_ext.php?oid=203859108&id=164810267&hash=60074b4f93d62429&hd=1
                        )

                )1

        )

)

我从一个更大的数组中切出以下内容。现在我想遍历这个数组并过滤一些值。我使用带有 true 选项的 sliced_array 功能来保留 [2] 数组编号。

现在这个数组有几个子数组,我需要一些值和一些过滤。在最终结果中,我想创建一个列出数组编号(1 到 12)的表,并在它后面有链接。 (所以一个 2 列表)我不需要的是剧集编号的第二个数组。这始终是剧集中的 [1] 数组。这是“俄语”部分,我在最终结果中不需要它。

我如何遍历它并得到一个简单的 html 表格作为输出?我一直在尝试使用 foreach() 方法,但现在我得到的只是“Array”。

编辑 答案确实是做多个foreach。

<?php
    $json = file_get_contents("http://trendico.ru/api/moviebox/get_tv_data?id=1&season=7");
    $array = json_decode($json, TRUE);


//Create an index array
$indexarray = (array_values($array));

//Create an array with only the episodes 
$episodes = array_slice($indexarray, 2, 1);



foreach($array as $arrayl1) {
    foreach($arrayl1 as $arrayl2) {
        foreach($arrayl2 as $arrayl3) {
            foreach($arrayl3 as $key => $val) {
                echo "$key=>$val"."<br>";
            }
        }
    }
}

?>

最佳答案

使用递归函数和is_array

function doSomething(&$complex_array){
foreach ($complex_array as $n => $v)
{
    if (is_array($v))
        doSomething($v);
    else
        do whatever you want to do with a single node
}}

关于php - 遍历索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16102323/

相关文章:

c++ - 用于多个 3d 阵列的 MPI_Scatterv 和 MPI_Gatherv

javascript - 如何使用javascript中reduce函数中的属性值跳过重复对象?

php - Yii2 一种形式的多个模型

php - 单独的 Angular 2 客户端和 Laravel PHP 后端的推送通知

android - 视频未在 Webview 中显示

javascript - 使下拉菜单可滚动

php - 如何使用复选框将 $1.00 添加到 $amount

java - 在 PHP/Java EE 混合服务器端环境中从 PHP 访问数据库

HTML/CSS - 左对齐并在同一行居中

javascript - 有没有一种简单的方法可以循环遍历一个对象,该对象的值是对象数组,以从这些对象的值生成列表