如何遍历此嵌套数组?

时间:2013-07-22 12:08:40

标签: php arrays loops multidimensional-array

如何使用PHP循环下面的数组以显示列表?列表中可能有超过7个项目。我应该使用foreachwhile吗?

我想要的输出是:

APPLE
PEAR
BANANA
etc 

这是数组结构:

Array  
(  
    [main_category] => Array  
        (  
            [0] => APPLES  
            [1] => PEARS  
            [2] => BANANAS  
            [3] => PEACHES  
            [4] => ORANGES  
            [5] => KIWIS  
            [6] => PASSION FRUITS  
        )      
)

2 个答案:

答案 0 :(得分:1)

foreach($your_array as $vals){
   foreach($vals['main_category'] as $val){
      echo $val;
      echo "<br>";
   }
}

答案 1 :(得分:0)

如果您只想要main_category

中的数据,则可以执行此操作
foreach($array['main_category'] as $value){ 
    //do whatever you want with the  value
    echo $value;
}

预期产出:

PEARS BANANAS PEACHES ORANGES KIWIS PASSION FRUITS