先按部门排序取出无限极架构数据(此时为一维数组),并将每个员工以”Satff”健值放入对应部门数组中,并将人数使用count以健值statistics放入数组中,此时数据为
[
{
“depart_id”: 1,
“customer_id”: 41,
“parent_id”: 0,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 2,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 6,
“Staff”: [
{
“real_name”: “李四”
},
{
“real_name”: “999”
},
{
“real_name”: “234”
},
{
“real_name”: “李四”
},
{
“real_name”: “1231111”
},
{
“real_name”: “测试23”
}
]
},
{
“depart_id”: 3,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 1,
“Staff”: [
{
“real_name”: “张三2”
}
]
},
{
“depart_id”: 4,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 5,
“Staff”: [
{
“real_name”: “yz测试专用1”
},
{
“real_name”: “yz测试003”
},
{
“real_name”: “yz测试00666”
},
{
“real_name”: “yz测试010”
},
{
“real_name”: “yz测试009”
}
]
},
{
“depart_id”: 5,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 1,
“Staff”: [
{
“real_name”: “张三11”
}
]
},
{
“depart_id”: 6,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 7,
“customer_id”: 41,
“parent_id”: 2,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 8,
“customer_id”: 41,
“parent_id”: 2,
“statistics”: 9,
“Staff”: [
{
“real_name”: “11111(详细)”
},
{
“real_name”: “122”
},
{
“real_name”: “张三三1111111111”
},
{
“real_name”: “张三三”
},
{
“real_name”: “张三三”
},
{
“real_name”: “1212”
},
{
“real_name”: “张三三”
},
{
“real_name”: “11111”
},
{
“real_name”: “zhansan”
}
]
},
{
“depart_id”: 9,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 10,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 11,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 17,
“customer_id”: 41,
“parent_id”: 7,
“statistics”: 0,
“Staff”: []
},
{
“depart_id”: 18,
“customer_id”: 41,
“parent_id”: 3,
“statistics”: 1,
“Staff”: [
{
“real_name”: “yz001”
}
]
},
{
“depart_id”: 23,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 2,
“Staff”: [
{
“real_name”: “张三”
},
{
“real_name”: “萨达”
}
]
},
{
“depart_id”: 24,
“customer_id”: 41,
“parent_id”: 1,
“statistics”: 0,
“Staff”: []
}
]
以上数据已变量 $arrDepartment 为例,然后为了破坏原有排序,将 数据放入临时变量,重新排序一下数据,具体如下
// 为了不破坏原来排序,使用临时变量
$arrDepartmentTmp = $arrDepartment;
// 按照子集ID从大到小重新排序
$arrTmp = array_column($arrDepartmentTmp, 'parent_id');
array_multisort($arrTmp, SORT_DESC, $arrDepartmentTmp);
// 为了还原排序,临时变量与原有变量均用他ID作为健值
$arrDepartmentTmp = array_column($arrDepartmentTmp, null, 'depart_id');
$arrDepartment = array_column($arrDepartment, null, 'depart_id');
然后进行人数统计累加
// 使用父级ID作为健值进行统计人数
foreach ($arrDepartmentTmp as $intDepartmentKey => &$arrDepartmentItem) {
//如果他父级ID为0,那意味着它是最高层,所以跳过
if ($arrDepartmentItem['parent_id'] != 0) {
//将它的人数加到他父级的人数统计中
$arrDepartmentTmp[$arrDepartmentItem['parent_id']]['statistics'] += $arrDepartmentItem['statistics'];
}
}
还原原来的排序
// 为了不破坏排序,按照原来排序重新组装
foreach ($arrDepartmentTmp as $k => $item) {
$arrDepartment[$k] = $item;
}
unset($arrDepartmentTmp);
转为树形数据
$arrTree = toTree($arrDepartment, 'depart_id', 'parent_id');
toTree函数在这 https://www.bugquit.com/487.html
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
一颗大萝北
本文地址: PHP无限级组织架构转为树形数据/菜单,并统计每个部门层级包含的子集包含的人数(父级子集的人数总和)并保持排序
本文地址: PHP无限级组织架构转为树形数据/菜单,并统计每个部门层级包含的子集包含的人数(父级子集的人数总和)并保持排序