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

  • 相关阅读:
    this和$(this)的关系
    单色边表格
    php概率算法
    jQuery Ajax 参数解析
    拍拍CPS入门使用
    飞鸽端口被占
    浏览器调试工具技巧分享
    事件click,bind,click
    jQuery旋转插件—rotate
    利用谷歌API生成二维码
  • 原文地址:https://www.cnblogs.com/JdsyJ/p/10954288.html
Copyright © 2011-2022 走看看