我试图遍历以下使用foreach的数据集,但有些原因我无法弄清楚什么是错的?
代码:
foreach($data['service']['products'] as $settings){
print_r($settings);
}
$ data as print_r
Array
(
[service] => Array
(
[products] => Array
(
[name] => OW VoIP
[settings] => Array
(
[0] => Array
(
[name] => ComplexPort
[value] => False
)
[1] => Array
(
[name] => LosingCarrier
[value] => 2
)
数据为var_dump
array (size=3)
'service' =>
array (size=1)
'products' =>
array (size=2)
'name' => string 'VoIP' (length=7)
'settings' =>
array (size=21)
0 =>
array (size=2)
'name' => string 'ComplexPort' (length=11)
'value' => string 'False' (length=5)
1 =>
array (size=2)
'name' => string 'LosingCarrier' (length=13)
'value' => string '2' (length=1)
答案 0 :(得分:0)
foreach([arrayLvL1][arrayLvL2][arrayLvL3] as $key => $value){
echo $value
}
我认为应该这样做。
评论中也有人说同样的话。我不确定如何链接到他们的评论。
答案 1 :(得分:0)
应该是
foreach($data['service']['products']['settings'] as $settings){
答案 2 :(得分:0)
这应该可以让您循环浏览所有产品及其设置:
foreach($data['service'] as $products){
foreach($products as $settings) {
if (is_array($settings))
print_r($settings);
}
}
答案 3 :(得分:-1)
我没有看到你得到的结果。你在打扰我们吗?你的结果不可能。
$arr = array(
'service' => array(
'products' => array(
'name' => 'VoIP',
'settings' => array(
0 => array(
'name' => 'ComplexPort',
'value' => 'False'
),
1 => array(
'name' => 'LosingCarrier',
'value' => 2
)
)
)
)
);
foreach($arr['service']['products']['settings'] as $settings) {
echo '<pre>' . print_r($settings) . '</pre>';
}
将输出
Array ( [name] => ComplexPort [value] => False )
1
Array ( [name] => LosingCarrier [value] => 2 )
1
基本上......你只需要处理一个阵列,产品或设置。它无法获取基本数组,因为它不存在于您提供foreach
事件的范围内。无论是谁投票,都是可悲的,也不了解阵列。