0

I have an array named as $reports. Which is in following format. When i tried to see using print_r($reports) in my view file.

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
                            )

                    )

            )

       )
)

I just showed here one x array like that i have so many arrays i want to loop through all of them. can please any one give me an idea how to do it.

4

7 回答 7

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

尝试这个..

于 2012-11-12T12:01:45.213 回答
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;
        }
    }
于 2012-11-12T11:46:54.250 回答
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
                }
            }
        }
    }
}
于 2012-11-12T11:43:02.710 回答
0
foreach($reports as $key1=>$val1)
{
    foreach($val1 as $key2=>$val2)
    {
        foreach($val2 as $key3=>$val3)
        {
            echo $val;
        }
    }
}
于 2012-11-12T11:37:12.070 回答
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";
        }
    }
}

?>
于 2012-11-12T11:37:43.100 回答
0

就像是:

array_walk_recursive($reports,
    create_function('$e','if (is_scalar($e)) echo sprintf("%s\n",$e);'));
于 2012-11-12T11:38:12.417 回答
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
于 2012-11-12T11:40:26.087 回答