如何使用foreach PHP循环3维数组

时间:2012-03-28 16:21:17

标签: php arrays foreach

下面是foreach和3维数组,循环没有问题,但我不能分析哪个数组要回显,他们必须像echo $ subvalue一样整个数组,有循环3维数组的更好的解决方案吗?我觉得这个循环感觉很奇怪。谢谢你

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] => aaa@hotmail.com
            [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] => aaa@hotmail.com
        [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] => aaa@hotmail.com
        [sid] => 268
        [story_name] => Story A
        [photo_url] => Array
            (
                [0] => story_photos/2012/0322/361332381393552719.jpg
            )

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

    )

4 个答案:

答案 0 :(得分:3)

为什么不试试这个:

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 />";
    }
}

另外,我不确定,因为你的问题不是很清楚或没有明确规定。

答案 1 :(得分:3)

或者

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

适用于N维数组

一个改进版本,用于了解深度并使用不同的css类进行深度处理,并使用设置应添加值的标记:

例如:对于深度0,类将通过arrayclass_0,深度为1 arrayclass_1等...

/**
$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>";
            }
        }
    }
}

答案 2 :(得分:0)

这并没有真正回答你的问题,但请注意,你的第三个循环是:

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

但是你已经在第一个循环中使用了$ key:

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

您应该将第三个更改为:

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

答案 3 :(得分:0)

您是否只想循环访问photo_urls和其他数据?如果是这种情况,那么这里是一个如何访问数据的简单示例:

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 />';
    }

}

如果您想以递归方式搜索数组,那么其他答案就是您想要的。