THINKPHP5.1随机获取数据库几条数据

function get random_data($table,$where,$num){
    
        $countcus = db($table)->where($where)->count();
        $min = db($table)->where($where)->min('id');
        $max = $min+$countcus;
        if($countcus < $num){$num = $countcus;}
        $i = 1;
        $flag = 0;
        $ary = array();
        while($i<=$num){
            $rundnum = rand($min, $max);
            if($flag != $rundnum){
                if(!in_array($rundnum,$ary)){
                    $ary[] = $rundnum;
                    $flag = $rundnum;
                }else{
                    $i--;
                }
                $i++;
            }
        }
        $where[] = ['id','in',$arr];
        $data = db($table)->where($where)->select();
        return $data;
}


你可能感兴趣的:(学习笔记,php)