php中递归查找父级名称

    /**
     * 获取所属公司
     * @param  array 列表
     * @param  $id   上级ID
     * @return  array
     */
     private static function get_top_company($company, $id)
     {
            $arr = [];
            foreach($company as $v) {
                if($v['id'] == $id) {
                    $arr[] = $v;
                    $arr = array_merge(self::get_top_company($company, $v['upid']), $arr);
                }
            }
            return $arr;
      }
        $list = [];
        foreach ($data as $k => $v) {
            $company = self::get_top_company($this->_getCompanyList(), $v['company']);
            $list[$k]['id'] = $v['id'];
            $list[$k]['name'] = $v['name'];
            $list[$k]['code'] = $v['code'];
            $list[$k]['mobile'] = $v['mobile'];
            $list[$k]['position'] = $v['position'];
            $list[$k]['reg_time'] = date("Y-m-d H:i", $v['reg_time']);
            $list[$k]['company'] = join('-', array_column($company, 'company_name', 'id'));
        }

 

posted @ 2018-06-08 18:02  ノGHJ  阅读(2130)  评论(0编辑  收藏  举报