1

I have a output of print_r below and I want to access all the individual elements value with foreach loop but unfortunately I am unable to do that via foreach. Could anyone please help me with the associate array question. I can access via this $arr['Level1'][Date] and it returns value as "2015-04-14 07:15".

But how to get all the element values via foreach loop?

Array
(
    [Level1] => Array
        (
            [Date] => 2015-04-14 07:15
            [img1] => pic1
            [img2] => pic2
            [InnerLevel] => Array
                (
                    [0] => value1
                    [1] => value2
                )

        )


    [Level2] => Array
        (
            [Date] => 2015-04-15 08:15
            [img1] => pic1
            [img2] => pic2
            [InnerLevel] => Array
                (
                    [0] => value3
                    [1] => value4
                )

        )

)
Mike Laren
  • 8,028
  • 17
  • 51
  • 70
newbie83
  • 53
  • 2
  • 10

4 Answers4

0
foreach ($myarray as $item) {
    echo $item['Date'] . "\n";
}

The fact that the array is associative or not doesn't change anything.

$item is successively a copy of $myarray['Level1'] then $myarray['Level2'] (etc. if more) in the foreach loop.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • Thank you Casimir! but "Level1" and "Level2" above are also dynamic value so i cannot say $myarray['Level1']. also there could be more level(1...n)? – newbie83 Apr 15 '15 at 02:55
  • @newbie83: it's not a problem, this way ignore totally what are the item "names" ("keys" is the good term), but if you want to know that, use `foreach ($myarray as $key=>$item)` instead, where `$key` contains the key name (so `Level1` for example). – Casimir et Hippolyte Apr 15 '15 at 03:10
0

It depends on index depth.

To extract simple associative arrays like:

$mainarray['Name']='Value';

Use:

foreach ($mainarray as $aname=>$avalue)
{
    echo $aname." in ".$mainarray." = ".$avalue." <br>";
}

To extract deeper associative arrays like:

$mainarray['Child']['Name']='Value';

Use:

foreach ($mainarray as $aname=>$asubarray)
{
    echo "In ".$aname." from ".$mainarray."...<br>";
    foreach ($asubarray as $asubname=>$asubvalue){
        echo $asubname." = ".$avalue." <br>";
    }
    echo "<br>";
}

This:

<br>

represents new line. If you're running the code from a command line or you just want text only output, then use this for a new line:

\n
0
<?php
foreach ($myarray as $level => $itemArr) {

    if(is_array($itemArr)) {
        foreach ($itemArr as $levelArr) {
            if(is_array($levelArr)) {
                foreach ($levelArr as $key => $interlevelValue) {
                    echo $interlevelValue;
                }
            } else {
            echo $levelArr;
            }
        }
    } else {
        echo $itemArr;
    }
}
?>
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
Suman Singh
  • 1,379
  • 12
  • 20
0

Actually there is another way of doing this in php.

"Iterating over Multidimensional arrays is easy with Spl Iterators :

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

foreach($iterator as $key=>$value) {
    echo $key.' -- '.$value.'<br />';
}

This will iterate through all possible iterable objects. See

" - Quoted from - https://stackoverflow.com/a/2149106/1766122 answered by @Gordon

Community
  • 1
  • 1
Sony Mathew
  • 2,929
  • 2
  • 22
  • 29