zoukankan      html  css  js  c++  java
  • 一个小demo---递归计算子类下的某个值的总和

    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);

  • 相关阅读:
    204. 计数质数
    面试题 16.06. 最小差
    8. ubantu系统相关
    7. 理论
    6. 图像处理
    5.git
    4.Torch量化
    3.Torch优化工具
    2.DB算法及代码问题分析
    1. 显卡相关问题
  • 原文地址:https://www.cnblogs.com/JdsyJ/p/10954288.html
Copyright © 2011-2022 走看看