簡體   English   中英

如何使用foreach PHP循環3維數組

[英]How to loop 3 dimension array using foreach PHP

下面是 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
            )

    )

)

為什么不試試這個:

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

另外,我不確定,因為您的問題不是很清楚或沒有具體說明。

要么

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

這並不能真正回答您的問題,但請注意,您的第三個循環是:

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

但是您已經在第一個循環中使用了 $key:

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

您應該將第三個更改為:

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

您只是想循環訪問並訪問 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 />';
    }

}

如果您想遞歸搜索數組,那么其他答案就是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM