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.
$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);
Try this..
foreach($reports as $key1=>$val1)
{
foreach($val1 as $key2=>$val2)
{
foreach($val2 as $key3=>$val3)
{
echo $val;
}
}
}
<?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";
}
}
}
?>
Something like:
array_walk_recursive($reports,
create_function('$e','if (is_scalar($e)) echo sprintf("%s\n",$e);'));
Generic example.
// 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
something like that
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
}
}
}
}
}
i have created a php function, that will extract a nested array into new basic array:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.