答案 0 :(得分:0)
你让它变得复杂,你可以这样做
<?php
$arrs = [["tag"=>"one","level"=>1],["tag"=>"two","level"=>2],["tag"=>"three","level"=>3]];
$new = array();
function setLevel($single){
if($single['level']==1){
$new["tag"] = $single['tag'];
}else{
$single['level'] -= 1;
$new["array"] = setLevel($single);
}
return $new;
}
$temp = array();
foreach($arrs as $key=>$arr){
$temp = array_merge_recursive($temp,setLevel($arr));
}
print_r($temp);
?>
答案 1 :(得分:0)
你可以做一个递归函数。
$tree = setTree($array, 0);
function setTree($v, $i){
$tree = null;
$level = $v[$i]['level'];
if($level > 0){
$next = $i + 1;
if(isset($v[$next]))
$tree = array('0' => array('tag' => $v[$i]['tag'], 'array' => setTree($v, $next)));
else{
$tree = array('0' => array('tag' => $v[$i]['tag']));
}
}
return $tree;
}
print_r($tree);