vzhen
vzhen

Reputation: 11157

How to loop 3 dimension array using foreach PHP

Below is foreach and the 3 dimension arrays, no problem with the looping but i cannot sepcify which array to echo, they must me the whole arrays like echo $subvalue, any better solutions with looping 3 dimension array? i actually feel weird with this looping. Thanks in adv

foreach ($stories as $key => $story){
    //echo "<br />";
        foreach($story as $subkey => $subvalue){
            echo $subvalue."<br />";
                foreach($subvalue as $key => $subsubvalue){
                    echo $subsubvalue."<br />";
                }
        }
}

Array
(
    [270] => Array
        (
            [uid] => 36
            [user_email] => [email protected]
            [sid] => 270
            [story_name] => Story C
            [photo_url] => Array
                (
                    [0] => story_photos/2012/0322/361332381418153311.jpg
                    [1] => story_photos/2012/0322/361332393792911587.jpg
                )

            [photo_added_date] => Array
                (
                    [0] => 1332381418
                    [1] => 1332393792
                )

        )

    [269] => Array
        (
        [uid] => 36
        [user_email] => [email protected]
        [sid] => 269
        [story_name] => Story B
        [photo_url] => Array
            (
                [0] => story_photos/2012/0322/361332381406580761.jpg
            )

        [photo_added_date] => Array
            (
                [0] => 1332381406
            )

    )

[268] => Array
    (
        [uid] => 36
        [user_email] => [email protected]
        [sid] => 268
        [story_name] => Story A
        [photo_url] => Array
            (
                [0] => story_photos/2012/0322/361332381393552719.jpg
            )

        [photo_added_date] => Array
            (
                [0] => 1332381393
            )

    )

)

Upvotes: 2

Views: 14847

Answers (4)

grifos
grifos

Reputation: 3381

Or

function echoArray( $array )
{
    foreach ($array as $key => $cell)
    {
        if ( true == is_array($cell) )
        {
           echoArray($cell);
        }
        else
        {
            echo "$cell<br />";
        }
    }
}

It works for N dimensionnal array

An improved version to know the depth and use different css class for depth and to use set the tag in which the value should be added:

Eg: for depth 0 the class will by arrayclass_0, for depth 1 arrayclass_1, etc...

/**
$array : The array
$depth: The depth ( you should always set it to 0)
$cssclassprefix: The css class prefix you want to set
$tag: the default tag to use to render
$arraytagkey: An optionnal key in your array to extract the tag to use
*/
function echoArray( $array, $depth=0, $cssclassprefix='arrayclass_', $tag='div', $arraytagkey = '' )
{
    if ( 0 != strcmp($arraytagkey) && isset($array[$arraytagkey]) )
    {
       $tag=$array[$arraytagkey];
    }
    foreach ($array as $key => $cell)
    {
        if ( true == is_array($cell) )
        {
           echoArray($cell, $depth+1, $cssclassprefix, $tag, $arraytagkey);
        }
        else
        {
            $matches = array();
            if ( 0 != preg_match("/^(img|iframe|input)$/i",$tag) )
            {
                if ( 0 != strcasecmp('input',$tag) )
                {
                   echo "<input class='$cssclassprefix$depth' value='$cell' />";
                }
                else
                {
                   echo "<$tag class='$cssclassprefix$depth' src='$cell' />";
                }
            }
            else if( 0 != preg_match("/^(br|hr)$/i",$tag) )
            {
                echo "$cell<$tag />";
            }
            else
            {
                echo "<$tag class='$cssclassprefix$depth'>$cell</$tag>";
            }
        }
    }
}

Upvotes: 3

MrCode
MrCode

Reputation: 64526

Are you just wanting to loop through and get access to the photo_urls and other data? If that's the case then here's a simple example of how you can access the data:

foreach($stories as $key => $story)
{

    // here you can echo the $store['user_email']  etc
    echo 'Email: ' . $story['user_email'] . '<br />';

    // loop over the photo urls
    foreach($story['photo_url'] as $photo_url)
    {
        echo 'Photo URL: ' . $photo_url . '<br />';
    }

    // loop over the photo added dates
    foreach($story['photo_added_date'] as $photo_added_date)
    {
        echo 'Photo Date: ' . $photo_added_date . '<br />';
    }

}

If you're wanting to recursively search the array then the other answers are what you want.

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53563

This doesn't really answer your question, but note, your third loop is this:

foreach ($subvalue as $key => $subsubvalue){

But you've already used $key in the first loop:

foreach ($stories as $key => $story){

You should change the third to:

foreach ($subvalue as $subsubkey => $subsubvalue){

Upvotes: 0

David B&#233;langer
David B&#233;langer

Reputation: 7438

Why not try this :

foreach ($stories as $key => $story){
    if(is_array($story)){
        foreach($story as $subkey => $subvalue){
            if(is_array($subvalue)){
                foreach($subvalue as $key => $subsubvalue){
                    echo $subsubvalue."<br />";
                }
            } else {
                echo $subvalue."<br />";
            }
        }
    } else {
        echo $story."<br />";
    }
}

Also, I am not sure because your question isn't really clear or specified.

Upvotes: 4

Related Questions