
public function demo($frames)
{
foreach ($frames as $k => $frame) {
$frames[$k]['allCount'] = $frame['employeeCount'];
if ($frame['children']) {
$frames[$k]['children'] = $this->demo($frame['children']);
$frames[$k]['allCount'] += array_sum(array_column($frames[$k]['children'],'allCount'));
}
}
return $frames;
}
public function index()
{
$arr = [[
'id'=>1,
'name'=>'就是',
'employeeCount'=>10,
'children'=>[[
'id'=>1,
'name'=>'就是1',
'employeeCount'=>6,
'children'=>[[
'id'=>1,
'name'=>'就是2',
'employeeCount'=>4,
'children'=>[
],
]],
'id'=>2,
'name'=>'就是3',
'employeeCount'=>11,
'children'=>[[
'id'=>1,
'name'=>'就是3',
'employeeCount'=>0,
'children'=>[[
'id'=>1,
'name'=>'就是4',
'employeeCount'=>2,
'children'=>[[
'id'=>1,
'name'=>'就是21',
'employeeCount'=>4,
'children'=>[
],
]],
]],
]],
]],
]];
$res = $this->demo($arr);dd($res);
本文介绍了一个PHP函数,用于递归地计算一个多级部门结构中所有员工的总数。通过遍历部门及其子部门,该函数能准确计算出每个部门及其下属部门的员工总数。
1315

被折叠的 条评论
为什么被折叠?



