循环索引数组

时间:2013-04-19 10:15:24

标签: 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

        )

)

我有以下内容,我从一个更大的阵列切片。现在我想循环通过这个数组并过滤一些值。我使用了slice_array特性和true选项来保存[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 个答案:

答案 0 :(得分: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
}}

答案 1 :(得分: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

答案 2 :(得分: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的数组。我不知道为什么!?但仍然循环通过数组内的数组。

答案 3 :(得分:1)

<?php
foreach($array as $smaller){
  foreach($smaller as $link){
     echo $link[0]['id'] . " > ". $link[0]['link']
  }
}