$tree = [
array(
'name' => 'One1',
),
array(
'name' => 'One2',
),
array(
'name' => 'One3',
),
array(
'name' => 'One4',
'children' => [
array(
'name' => 'Two1',
),
array(
'name' => 'Two2',
'children' => [
array(
'name' => 'Three1',
),
],
),
array(
'name' => 'Two3',
),
]
),
array(
'name' => 'One5',
),
];
function getValuesPaths(array $tree, $glue = ' > ') {
$branches = array();
foreach ($tree as &$item) {
$piece = $item['name'];
if (array_key_exists('children', $item)) {
if (count($item['children'])>1) {
$leafs = self::getValuesPaths($item['children']);
foreach ($leafs as $item) {
$branches[] = $piece . $glue . $item;
}
}
} else {
$branches[] = $piece;
}
}
return $branches;
}
这输出以下路径:
我们不应该找到One4 > Two2 > Three1
?
答案 0 :(得分:0)
可以帮助你......
function getKeyPaths(array $tree, $glue = '.')
{
$paths = array();
foreach ($tree as $key => &$mixed) {
if (is_array($mixed)) {
$results = getKeyPaths($mixed, $glue);
foreach ($results as $k => &$v) {
$paths[$key . $glue . $k] = $v;
}
unset($results);
} else {
$paths[$key] = $mixed;
}
}
return $paths;
}