我想在php
中遍历数组的所有键[Field_1] => SOME VALUE
[Field_2] => Array
(
)
[Field_3] => Array
(
)
[Field_4] => SOME VALUE
[Field_5] => Array
(
)
[Field_6] => Array
(
)
[Field_7] => SOME VALUE
[Field_8] => SOME VALUE
[Field_9] => SOME VALUE
[Field_10] => Array
(
[Field_10.1] => Array
(
[0] => Array
(
[key_1] => SOME VALUE
[key_2] => SOME VALUE
[key_3] => SOME VALUE
[key_4] => Array
(
)
[key_5] => SOME VALUE
[key_6] => SOME VALUE
(
)
)
[1] => Array
(
[key_1] => SOME VALUE
[key_2] => SOME VALUE
[key_3] => SOME VALUE
[key_4] => Array
(
)
[key_5] => SOME VALUE
[key_6] => Array
(
)
)
[2] => Array
(
[key_1] => SOME VALUE
[key_2] => SOME VALUE
[key_3] => SOME VALUE
[key_4] => Array
(
)
[key_5] => SOME VALUE
[key_6] => Array
(
)
)
)
)
[Field_11] => Array
(
)
如何遍历上面数组中的所有键和嵌套键。最好使用递归函数,我想遍历数组并找出数组中的特定键。
答案 0 :(得分:1)
function traverse_keys($arr) {
foreach($arr as $key => $value) {
if(is_array($value) && !empty($value)) {
echo "<p style='font-weight: bold'>{$key}</p>";
traverse_keys($value);
} else {
echo "<p>{$key}</p>";
}
}
}
traverse_keys($arr);
此代码打印出所有外键和内键。以粗体显示的键是子数组的父键。希望它有所帮助!