How to loop 3 dimension array using foreach PHP

13,460

Solution 1

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.

Solution 2

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>";
            }
        }
    }
}
Share:
13,460
vzhen
Author by

vzhen

Updated on June 04, 2022

Comments

  • vzhen
    vzhen almost 2 years

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

    )