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

  • 相关阅读:
    chlick 在 blur 之后触发
    屏蔽运营商广告
    script标签清除缓存
    http-equiv 详解
    jqLite
    js 时间戳和转换-转载
    JS数组的常用方法
    js 前端实现文件流下载的几种方式
    解决兼容性的库
    移动端兼容性问题
  • 原文地址:https://www.cnblogs.com/JdsyJ/p/10954288.html
Copyright © 2011-2022 走看看