Fully recursive solution, I use it when I need to merge 2 glob arrays. All duplicates inserted only once.
function add_path_to_array($path, $array) {
$path = trim($path, '/');
$exploded = explode('/',$path);
if (count($exploded) > 1) {
$key = $exploded[0];
if (!isset($array[$key]))
$array[$key] = array();
$array[$key] = array_replace_recursive($array[$key], add_path_to_array(substr($path, strpos($path, '/')), $array[$key]));
}
else
if (!in_array($path, $array))
$array[] = $path;
return $array;
}
$list = array(
'/a/b/aaaa.txt',
'/c/d/bbbb.txt',
'/a/z/dddd.txt',
'/a/b/c/d/yyyy.txt',
);
$nested_array = array();
foreach($list as $path)
$nested_array = add_path_to_array($path, $nested_array);
var_dump($nested_array);