About an array conve & ion

60 0
$a = array('dir1', 'dir2', 'dir3', 'file1');
$b = array('dir1', 'dir2', 'dir3', 'file2');
//上面的数组怎么变成下面这种
/*
array(
 'dir1'=>array(
 'dir2'=>array(
 'dir3'=>array('file1', 'file2',)
 )
 )
);
*/

An algorithm, thank you.

1 Answers

87 0

All right, ~, I'll get out of it.

$a = array('dir1', 'dir2', 'dir3', 'file1');
function build($tree, $arr) {
 if(count($arr) == 1) {
 return $arr;
 }
 $key = array_shift($arr);
 $tree[$key] = build($tree, $arr);
 return $tree;
}
$tree = array();
print_r(build($tree, $a));
...