簡體   English   中英

Foreach遍歷這樣的多維數組

[英]Foreach loop through multidimentional array like this

我在變量$comments有一個多維數組,其中包含:

Array (
    [0] => Array
        (
            [0] => 889
            [1] => First comment
            [2] => 8128912812
            [3] => appr
        )

    [1] => Array
        (
            [0] => 201
            [1] => This is the second comment
            [2] => 333333
            [3] => appr
        )

    // There is more...
)

如何遍歷此數組並為每個回顯每個值?

foreach($arrayOfArrays as $array){
    foreach($array as $index => $value){
        echo $array[$index];
    }
}

您應該使用兩個foreach循環,因為您的數組必須級別:

foreach ($comments as $comment)
   foreach ($comment as $comment_data)
       echo $comment_data;

如果您的數組structre保持顯示的樣子,則可以按照以下步驟進行操作:

foreach($comments as $comment) {
    echo $comment[0];
    echo $comment[1];
    echo $comment[2];
    echo $comment[3];
}

只需使用兩個foreach循環。 一個里面另一個

foreach($comments as $commentArray){
    foreach($commentArray as $comment){
        echo $comment; 
    }
}

希望這對您有幫助

$i=0;
$c=count($array);
while ($i<$c) {
   foreach ($array[$i] as $comment_property) {
echo $comment_property;
}
$i++;
}

暫無
暫無

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

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