我有一个包含数组的数组。我需要遍历它,所以我将索引作为变量,并将值作为一个。
我在我的阵列上运行print_r($array)
并显示:
Array
{
[ns] => Array
{
[car1]=>chevy
[car2]=>dodge
}
[mx] =>Array
{
[color1]=>red
}
}
我需要循环显示:
car1 chevy
car2 dodge
color1 red
我知道我需要一个foreach
循环,但我似乎无法让它正常工作。我总是得到错误:
尝试获取非对象的属性
答案 0 :(得分:2)
您将在下面找到如何循环键和值的示例。
<?php
foreach($yourArr as $key => $value){
//$key will contain ns and mx
//$value will contain Array and Array
//So we need to loop through $value
foreach($value as $subKey => $subValue){
//$subKey will contain car1, car2 and color1
//$subValue will contain chevy, dodge, red
}
}
答案 1 :(得分:1)
遍历数组数组以获取内部数组的值的方式如下:
foreach($array as $row => $subArray)
{
foreach($subArray as $subRow => $value)
{
echo $value . "<br/>";
}
}