$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
?
Может помочь вам…
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; }
не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; }