codeigniter 如何循环遍历这个复杂的关联数组?[duplicate]

7y4bm7vi  于 2022-12-07  发布在  其他
关注(0)|答案(7)|浏览(146)

此问题在此处已有答案

How can I access an array/object?(6个答案)
8个月前关闭。
我有一个名为$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数组,我有很多数组,我想遍历所有的数组,请大家给予我一个想法。

ctrmrzij

ctrmrzij1#

$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);

试试这个

z4bn682m

z4bn682m2#

<?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";
        }
    }
}

?>
kxxlusnw

kxxlusnw3#

一般示例。

// 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
czfnxgou

czfnxgou4#

类似的东西

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
                }
            }
        }
    }
}
xam8gpfp

xam8gpfp5#

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

brccelvz6#

我已经创建了一个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;
        }
    }
q8l4jmvw

q8l4jmvw7#

类似于:

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

相关问题