遍历多维数组 [英] Traverse multi-dimensional array
本文介绍了遍历多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有下面的数组,需要访问id,但无法使其正常工作.不知道如何访问数组的第3级.
Have the array below and need to access id but can't get it to work. Don't know how to access 3rd level of array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[id] => 3
)
)
[3] => Array
(
)
[4] => Array
(
[0] => Array
(
[id] => 5
)
)
}
推荐答案
在您的示例中,似乎id
始终与key of 0
;
In your example it looks as though id
is always in a separate array with the key of 0
;
1,硬编码:
foreach($array as $value){
echo isset($value[0]['id']) ? $value[0]['id'] : '';
}
2,遍历第二个数组:
2, iterate though second array:
foreach($array as $key=>$value){
if(is_array($value)){
foreach($value as $v){
if(isset($v['id'])){
echo $v['id'];
}
}
}
}
这篇关于遍历多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文