2
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() 方法,但现在我得到的只是“数组”。

编辑 答案确实是做多个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>";
            }
        }
    }
}

?>
4

4 回答 4

3

使用递归函数和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
}}
于 2013-04-19T10:19:58.763 回答
2

您可能不够深入,这应该可以:

foreach($array[2] as $link){
      if($choice == 'eng'){
          // the English
          echo $link[0]['id'] . " - ". $link[0]['link'];
      }
      elseif($choice == 'ru'){
          // the Russian
          echo $link[1]['id'] . " - ". $link[1]['link'];
      }
}

$array 是用于存储多维数组的变量,第一个元素是键为 2 的数组。我不知道为什么!?但仍然循环通过数组内部的数组。

于 2013-04-19T10:31:44.140 回答
2

您可以在所有数组中循环,直到获得所需的数据,因为数据位于第 4 级,所以让我们循环 4 次

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

这将输出

id=>24339
active=>1
lang=>en
link=>http://vk.com/video_ext.php?oid=203859683&id=165118953&hash=545584bd3253fab9&hd=1
id=>24483
active=>1
lang=>ru // and so on
于 2013-04-19T10:28:39.637 回答
1
<?php
foreach($array as $smaller){
  foreach($smaller as $link){
     echo $link[0]['id'] . " > ". $link[0]['link']
  }
}
于 2013-04-19T10:19:52.150 回答