您的位置:首页 > 其它

具有上下级关系的一维数组转化为多维

2019-08-09 09:11 253 查看
[code] public function deal_list_to_tree($data, $pkName='id', $pIdName='pid', $childName='children_list', $is_empty_childrens=false, $rootId=''){
$new_data = [];
foreach($data as $sorData){
if($sorData[$pIdName] == $rootId){
$res = $this->deal_list_to_tree($data, $pkName, $pIdName, $childName, $is_empty_childrens, $sorData[$pkName]);
if(!empty($res) && !$is_empty_childrens){
if(array_key_exists($childName, $sorData)) {
if(array_key_exists($childName, $sorData)){
$sorData[$childName][] = $res[0];
}else{
$sorData[$childName][] = $res;
}
}else{
$sorData[$childName] = $res;
}
}
$new_data[] = $sorData;
}
}
return $new_data;
}

array(7) {
[0] => array(5) {
["id"] => int(1)
["action"] => string(12) "房源管理"
["url"] => string(0) ""
["pid"] => int(0)
["children_list"] => array(3) {
[0] => array(5) {
["id"] => int(8)
["action"] => string(15) "单元楼管理"
["url"] => string(0) ""
["pid"] => int(1)
["children_list"] => array(3) {
[0] => array(4) {
["id"] => int(11)
["action"] => string(6) "添加"
["url"] => string(22) "manage/staff/showStaff"
["pid"] => int(8)
}
[1] => array(4) {
["id"] => int(12)
["action"] => string(6) "删除"
["url"] => string(23) "manage/staff/showStaff1"
["pid"] => int(8)
}
[2] => array(4) {
["id"] => int(13)
["action"] => string(6) "编辑"
["url"] => string(23) "manage/staff/showStaff1"
["pid"] => int(8)
}
}
}
[1] => array(5) {
["id"] => int(9)
["action"] => string(18) "住房房源管理"
["url"] => string(0) ""
["pid"] => int(1)
["children_list"] => array(3) {
[0] => array(4) {
["id"] => int(14)
["action"] => string(6) "添加"
["url"] => string(22) "manage/staff/showStaff"
["pid"] => int(9)
}
[1] => array(4) {
["id"] => int(15)
["action"] => string(6) "删除"
["url"] => string(0) ""
["pid"] => int(9)
}
[2] => array(4) {
["id"] => int(16)
["action"] => string(6) "编辑"
["url"] => string(0) ""
["pid"] => int(9)
}
}
}
[2] => array(5) {
["id"] => int(10)
["action"] => string(18) "商铺房源管理"
["url"] => string(0) ""
["pid"] => int(1)
["children_list"] => array(3) {
[0] => array(4) {
["id"] => int(17)
["action"] => string(6) "添加"
["url"] => string(0) ""
["pid"] => int(10)
}
[1] => array(4) {
["id"] => int(18)
["action"] => string(6) "删除"
["url"] => string(0) ""
["pid"] => int(10)
}
[2] => array(4) {
["id"] => int(19)
["action"] => string(6) "编辑"
["url"] => string(0) ""
["pid"] => int(10)
}
}
}
}
}
[1] => array(4) {
["id"] => int(2)
["action"] => string(12) "租客管理"
["url"] => string(0) ""
["pid"] => int(0)
}
[2] => array(4) {
["id"] => int(3)
["action"] => string(12) "财务管理"
["url"] => string(0) ""
["pid"] => int(0)
}
[3] => array(4) {
["id"] => int(4)
["action"] => string(12) "公寓管理"
["url"] => string(0) ""
["pid"] => int(0)
}
[4] => array(4) {
["id"] => int(5)
["action"] => string(12) "营收统计"
["url"] => string(0) ""
["pid"] => int(0)
}
[5] => array(4) {
["id"] => int(6)
["action"] => string(15) "数据云存储"
["url"] => string(0) ""
["pid"] => int(0)
}
[6] => array(4) {
["id"] => int(7)
["action"] => string(27) "公寓行业大数据分享"
["url"] => string(0) ""
["pid"] => int(0)
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: