如何遍历这个复杂的关联数组?

时间:2012-11-12 11:29:37

标签: php codeigniter

我有一个名为$ reports的数组。其格式如下。当我试图在我的视图文件中看到使用print_r($ reports)时。

Array
(
[x] => Array
    (
        [A] => Array
            (
                [0] => Channel1 - 1 Stations
                [1] => n
                [2] => 1
                [3] => 2012-11-02 11:30:00
                [4] => 60
                [5] => 20
                [6] => 0
                [7] => 18
                [8] => 602
                [9] => 540
                [10] => Array
                    (
                        [0] => Array
                            (
                                [1] => Whirlpool Ice Magic 30.sec Tamil
                                [2] => 0
                                [3] => 20
                                [4] => 0
                                [5] => 18
                                [6] => 602
                                [7] => 540
                            )

                    )

            )

       )
)

我刚刚在这里展示了一个x数组,就像我有这么多数组我想循环遍历它们。可以请任何人给我一个如何做的想法。

7 个答案:

答案 0 :(得分:2)

$ary = array
        ("x" => array(
            "A" => array
                (
                0 => "Channel1 - 1 Stations",
                1 => "n",
                2 => 1,
                3 => "2012-11-02 11:30:00",
                4 => 60,
                5 => 20,
                6 => 0,
                7 => 18,
                8 => 602,
                9 => 540,
                10 => array
                    (
                    0 => array
                        (
                        1 => "Whirlpool Ice Magic 30.sec Tamil",
                        2 => 0,
                        3 => 20,
                        4 => 0,
                        5 => 18,
                        6 => 602,
                        7 => 540,
                    )
                )
            ),
            ));

    function fetchArray($ary, $level = 0)
    {
        if (is_array($ary))
        {
            $pad = $level * 25;

            foreach ($ary as $key => $val)
            {
                if (is_array($val))
                {
                    echo "<div style='padding-left:" . $pad . "px'>" . $key . " = Array</div>";

                    $level++;
                    fetchArray($val, $level);
                }
                else
                {
                    echo "<div style='padding-left:" . $pad . "px'>" . $key . " => " . $val . "</div>";
                }
            }
        }
    }

    fetchArray($ary, 0);

试试这个..

答案 1 :(得分:0)

foreach($reports as $key1=>$val1)
{
    foreach($val1 as $key2=>$val2)
    {
        foreach($val2 as $key3=>$val3)
        {
            echo $val;
        }
    }
}

答案 2 :(得分:0)

<?php

$array = ....;

foreach($array as $value)
{
    foreach($value as $valueInner)
    {
        if(is_array($valueInner))
        {
            foreach($valueInner as $valueInner2)
            {
                foreach($valueInner2 as $valueInner3)
                {
                    echo $valueInner3."\n";
                }
            }
        }
        else
        {
            echo $valueInner."\n";
        }
    }
}

?>

答案 3 :(得分:0)

类似的东西:

array_walk_recursive($reports,
    create_function('$e','if (is_scalar($e)) echo sprintf("%s\n",$e);'));

答案 4 :(得分:0)

通用示例。

 // your data
 $a = array(
     'a',
     'b',
     array (
         'c',
         'd',
         array ('e','f')
     )
 );

 // your recursive function
 function echo_row($x) {
     if (is_array($x)) {
         // if is an array calls the recursive function over the new array
         foreach ($x as $e) {
             echo_row($e);
         }
     } else {
         // prints a row, change to your table html
         echo $x . "<br>";
     }
 }

// execute the function 
echo_row($a);

// results
a
b
c
d
e
f

答案 5 :(得分:0)

类似的东西

foreach($reports as $report) {
    foreach($report as $element) {
        //$element[3] would give you 2012-11-02 11:30:00
        if(is_array($element) {
            foreach($element as $x) {
                foreach($x as $y) {
                    //$y[1] would give you Whirlpool Ice Magic 30.sec Tamil
                }
            }
        }
    }
}

答案 6 :(得分:0)

我创建了一个php函数,它将嵌套数组提取到新的基本数组中:

    function extractArray($nested_array,&$new_array)
    {
        foreach($nested_array as $key => $val)
        {
            if(is_array($val)) extractArray($val,$new_array);
            else $new_array[$key] = $val;
        }
    }