时间:2021-07-01 10:21:17 帮助过:1人阅读
转为
$brr = array(
'a'=>array(
'b'=>array(
'c'=>array(
'd'=>array()
)
)
)
);
$arr = array('a','b','c','d');
转为
$brr = array(
'a'=>array(
'b'=>array(
'c'=>array(
'd'=>array()
)
)
)
);
$child];
$child = $res;
}
print_r($res);
结果为
Array
(
[a] => Array
(
[b] => Array
(
[c] => Array
(
[d] => Array
(
)
)
)
)
)
看到结构想到了递归,具体代码如下:
function toMany ($arr) {
$res = array();
burnArr($res, $arr);
return $res;
}
function burnArr (&$arr, $keys) {
if ( !empty($keys) ) {
$val = array_shift($keys);
$arr[$val] = array();
burnArr($arr[$val], $keys);
} else {
return ;
}
}