我想从数组构建一个多维数组.比如我想
原文链接:https://www.f2er.com/php/132575.html$test = array ( 0 => 'Tree',1 => 'Trunk',2 => 'Branch',3 => 'Limb',4 => 'Apple',5 => 'Seed' );
成为
$test = array ( 'Tree' => array ( 'Trunk' => array ( 'Branch' => array ( 'Limb' => array ( 'Apple' => array ( 'Seed' => array () ) ) ) ) ) );
或者更简单
$result[Tree][Trunk][Branch][Limb][Apple][Seed] = null;
我试图用递归函数做这个,但是我达到内存限制,所以我显然做错了.
<?PHP $test = array ( 0 => 'Tree',5 => 'Seed' ); print_r($test); print "results of function"; print_r(buildArray($test)); function buildArray (&$array,&$build = null) { if (count($array) > 0) { //create an array,pass the array to itself removing the first value $temp = array_values($array); unset ($temp[0]); $build[$array[0]] = $temp; buildArray($build,$temp); return $build; } return $build; }