[英]Print multidimensional array's end-values with full keys path
I have multidimensional array, for example: 我有多维数组,例如:
$input = [
'a' => [
'b' => 'c',
'd' => 'e',
'f' => [
'g' => 'h'
],
],
'i' => 'j',
'k' => [
'l' => 'm'
],
];
I need to print all non-array values with the full key path to it. 我需要使用其完整键路径来打印所有非数组值。 Like this: 像这样:
a > b > c
a > d > e
a > f > g > h
i > j
k > l > m
How can I do this? 我怎样才能做到这一点?
You need to use a recursive function to traverse the levels of your array. 您需要使用递归函数遍历数组的级别。 This should give you your desired results: 这应该给您您期望的结果:
function list_paths($input) {
$paths = array();
foreach ($input as $k => $v) {
if (is_array($v)) {
foreach (list_paths($v) as $path) {
$paths[] = $k . " > " . $path;
}
}
else {
$paths[] = $k . " > " . $v;
}
}
return $paths;
}
print_r(list_paths($input));
Output: 输出:
Array (
[0] => a > b > c
[1] => a > d > e
[2] => a > f > g > h
[3] => i > j
[4] => k > l > m
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.