<?php
$t_tree = [];
$arr_route = ['a','b','c','d'];
array_tree_add_route($t_tree, $arr_route);
$arr_route = ['a','b','c','d', 'e'];
array_tree_add_route($t_tree, $arr_route);
$arr_route = ['x','b','c','e'];
array_tree_add_route($t_tree, $arr_route);
$arr_route = ['x','b','d','g'];
array_tree_add_route($t_tree, $arr_route);
echo '<pre> t_tree=<br>';
print_r($t_tree);
/**
* 数组转为树,添加路径
* @param $vTreeArray :树数组
* @param $vRoute :路径
*/
function array_tree_add_route(&$vTreeArray, $vRouteArray){
$tTreeArray = &$vTreeArray;
foreach($vRouteArray as $t_route){
$tTreeArray = &$tTreeArray[$t_route];
}
return $tTreeArray;
}
结果:
t_tree=
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
[e] =>
)
)
)
)
[x] => Array
(
[b] => Array
(
[c] => Array
(
[e] =>
)
[d] => Array
(
[g] =>
)
)
)
)