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
                )

        )

)
1
  • whats the exact output your after? Commented Apr 15, 2015 at 2:48

4 Answers 4

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;
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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: 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).
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.