如何使用PHP循环下面的数组以显示列表?列表中可能有超过7个项目。我应该使用foreach
,while
吗?
我想要的输出是:
APPLE
PEAR
BANANA
etc
这是数组结构:
Array
(
[main_category] => Array
(
[0] => APPLES
[1] => PEARS
[2] => BANANAS
[3] => PEACHES
[4] => ORANGES
[5] => KIWIS
[6] => PASSION FRUITS
)
)
答案 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