thinkphp中查询一个表中数据,根据两个字段

public static function systemPage($where,$adminId,$type = 1)
    {
        $model = self::alias('r');

        if (!isset($where['data']) || $where['data'] == '') {

            $where['data'] = 'today';
            
        }
        $model = self::getModelTime($where, $model, 'r.add_time');

        if($type == 1){

            $sids = self::getMid($adminId,0,4);

            $model = $model->whereIn('r.uid',$sids);

        }elseif($type == 2){

            $sids = self::getMid($adminId,0,4);

            $model = $model->whereIn('r.uid',$sids)->whereNotIn('r.level_id','3,4,5');

        }elseif($type == 3){

            $mid = School::where(['pid'=>$adminId,'status'=>2,'is_del'=>0])->column('id');
            
            Array_push($mid,$adminId);
            
            $model = $model->whereIn('r.uid',$mid);

        }elseif($type == 4){

            $mid = School::where(['pid'=>$adminId,'status'=>2,'is_del'=>0])->column('id');
            
            Array_push($mid,$adminId);
            
            $model = $model->whereIn('r.uid',$mid)->whereNotIn('r.level_id','3,4,5');
        }
        
        $data = $model->rightJoin('school s','r.uid = s.id')->field('r.uid,r.user_name,r.source_id,r.source')->order('s.pid desc')->select()->toArray();

        dump($data);exit();
      打印数据:
      array:30 [
          0 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          1 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]

          2 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]
          3 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          4 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          5 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]

          6 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          7 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 21
            "source" => "来源2"
          ]
          8 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 21
            "source" => "来源2"
          ]
          9 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]
          10 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 21
            "source" => "来源2"
          ]
          11 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 21
            "source" => "来源2"
          ]
          12 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]
          13 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 90
            "source" => "来源3"
          ]
          14 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]
          15 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]
          16 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 21
            "source" => "来源2"
          ]
          17 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]
          18 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]
          19 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 21
            "source" => "来源2"
          ]
          20 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]

          21 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]
          22 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          23 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]
          24 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          25 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]

          26 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          27 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]

          28 => array:4 [
            "uid" => 10
            "user_name" => "测试2"
            "source_id" => 77
            "source" => "来源1"
          ]

          29 => array:4 [
            "uid" => 14
            "user_name" => "测试1"
            "source_id" => 77
            "source" => "来源1"
          ]
        ]

        $list = [];   $info = [];

        foreach ($data as &$value) {

            $info[$value['uid']]['uid'] = $value['uid'];

            $info[$value['uid']]['user_name'] = $value['user_name'];

            $info[$value['uid']]['children'][] = $value;
        }

        foreach ($info as $k => $v) {

            $list[$k]['uid'] = $v['uid'];

            $list[$k]['user_name'] = $v['user_name'];

            foreach ($v['children'] as &$val) {

                $list[$k]['childrens'][$val['source_id']]['source_id'] = $val['source_id'];

                $list[$k]['childrens'][$val['source_id']]['source'] = $val['source'];

                $list[$k]['childrens'][$val['source_id']]['child'][] = $val;

            }
        }

        foreach ($list as &$item) {

            foreach ($item['childrens'] as &$index) {

                $index['pid'] = Source::where('id',$index['source_id'])->value('pid');

                $index['source_count'] = count($index['child']);

                unset($index['child']);

                $item['children'][] = $index;
            }

            unset($item['childrens']);
        }

        dump($list);exit();
打印结果:
       array:11 [
          14 => array:3 [
            "uid" => 14
            "user_name" => "测试1"
            "children" => array:2 [
              0 => array:4 [
                "source_id" => 77
                "source" => "来源1"
                "pid" => 4
                "source_count" => 13
              ]
              1 => array:4 [
                "source_id" => 21
                "source" => "来源2"
                "pid" => 1
                "source_count" => 4
              ]
            ]
          ]
          10 => array:3 [
            "uid" => 10
            "user_name" => "测试2"
            "children" => array:3 [
              0 => array:4 [
                "source_id" => 77
                "source" => "来源1"
                "pid" => 4
                "source_count" => 10
              ]
              1 => array:4 [
                "source_id" => 90
                "source" => "来源2"
                "pid" => null
                "source_count" => 1
              ]
              2 => array:4 [
                "source_id" => 21
                "source" => "来源3"
                "pid" => 1
                "source_count" => 2
              ]
            ]
          ]
        ]
    }
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章