多个编号重复,递归处理

编号重复

在数据库中有P20190199139编号,重复了将近几十个,所以循环处理,依次追加增1


public function tests()
    {
       $results=$this->db->query('select pro_sn from (SELECT * FROM `t_pro_info` where serialid=201901 GROUP by pro_sn having count(pro_sn)>1) as result')->result_array();
       $array_sn=array_column($this->db->select('pro_sn')->where('serialid',201901)->get('t_pro_info')->result_array(),'pro_sn');
       
        foreach($results as $vals) {
            $pros=$this->db->select('pro_sn,pro_id')->where('pro_sn',$vals['pro_sn'])->get('t_pro_info')->result_array();
            $i=0;
            foreach($pros as $xsa) {
                
                $i++;
                //$pros_sn='P'.(substr($xsa['pro_sn'],1)+$i);
                $pros_sn=$xsa['pro_sn'];
               
               
                if (empty($this->is_arrays($array_sn,$pros_sn))) {
                        continue;
                } else {

                    $sn=$this->is_arrays($array_sn,$pros_sn);
                    $this->db->where('pro_id',$xsa['pro_id'])->update('t_pro_info',array('pro_sn'=>$sn));
                    array_push($array_sn,$sn);
                    // $array_sn=array_column($this->db->select('pro_sn')->where('serialid',201901)->get('t_pro_info')->result_array(),'pro_sn');
                    
                }
                
                
                
            }


        }
        
    }

    public function is_arrays($arrs,$ned,&$j=0)
    {
        
        $j++;
        
        if (in_array($ned,$arrs)) {
            
               $pros='P'.(substr($ned,1)+$j);
              
              return  $this->is_arrays($arrs,$pros,$j);
        } else {
            //var_dump($ned);
            return $ned;
        }
    }


posted @ 2019-09-09 10:48  尘梦  阅读(324)  评论(0编辑  收藏  举报