Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何限制数组中的重复值_Php_Arrays - Fatal编程技术网

Php 如何限制数组中的重复值

Php 如何限制数组中的重复值,php,arrays,Php,Arrays,首先,我为我缺乏英语而道歉。我希望你明白我在这里要解释的 所以基本上我需要构建一个函数来限制数组中重复值的数量 我之所以需要这样做,是因为我正在构建一个系统,将数字分成若干组,每个组必须有相同数量的数字 编辑:随机数表示组号 我已经写了一个函数来实现这个功能,但是由于某些原因,它不能正常工作 function jagaTiimid($max, $liiget, $tArvLength, $tArv){ $tiimid = []; //Starting array for(

首先,我为我缺乏英语而道歉。我希望你明白我在这里要解释的

所以基本上我需要构建一个函数来限制数组中重复值的数量

我之所以需要这样做,是因为我正在构建一个系统,将数字分成若干组,每个组必须有相同数量的数字

编辑:随机数表示组号

我已经写了一个函数来实现这个功能,但是由于某些原因,它不能正常工作

function jagaTiimid($max, $liiget, $tArvLength, $tArv){
      $tiimid = []; //Starting array
      for($z=0;$z<$liiget;$z++){
          $numbers = [];
          $rn = randomNumber($tArvLength, $tArv, $numbers); //Generate a random number for a group, etc group 1, group 2, group 3 
          $mitu = countInArray($tiimid, $rn); //Check how many times that number has occured in array
          if($mitu == $max){ //If it equals to maximum number of times then... 
             $rnUus = randomNumber($tArvLength, $tArv, $numbers); //generate a new random number
             while($rnUus == $rn){
               $numbers = [];
               $rnUus = randomNumber($tArvLength, $tArv, $numbers);
             } //loop until the new generated number doesn't equal to old rn.
             $tiimid[] = $rnUus; //if it doesn't equal to $rn then push into array
          }else{
             $tiimid[] = $rn;
          }
      }
      return $tiimid;
}
函数jagaTiimid($max、$liiget、$tArvLength、$tArv){
$tiimid=[];//开始数组
对于($z=0;$z将while($rnUus==$rn)替换为while(countinaray($tiimid,$rnUus)>=$max)

–Ilya Bursov

当第一个随机选取命中一个已经使用
$liiget
次的数字时,内部循环开始,但它不会检查新生成的随机数是否已经出现
$liiget

为了提高效率,我会记录一个数字被使用的次数。此外,如果真的没有任何数字不会超过最大重复次数,你可以从安全网中受益

不需要嵌套循环。代码如下所示:

function jagaTiimid($max, $liiget, $tArvLength, $tArv){
    $tiimid = []; //Starting array
    $counts = []; // Helper for quick count
    $tries = 0; // Counter to avoid infinite looping
    while (count($tiimid) < $liiget && $tries++ < 100) {
        $numbers = [];
        $rn = randomNumber($tArvLength, $tArv, $numbers); //Generate a random number for a group, etc group 1, group 2, group 3 
        if (!isset($counts[$rn])) $counts[$rn] = 0; // initialise on first occurence
        if ($counts[$rn] < $max) {
            $tiimid[] = $rn; // add it to the result
            $counts[$rn]++; // ... and adjust the count
            $tries = 0; // reset the safety
        }
    }
    return $tiimid;    
}
函数jagaTiimid($max、$liiget、$tArvLength、$tArv){
$tiimid=[];//开始数组
$counts=[];//用于快速计数的帮助器
$trys=0;//避免无限循环的计数器
while(count($tiimid)<$liiget&$trys++<100){
$numbers=[];
$rn=randomNumber($tArvLength,$tArv,$numbers);//为组生成一个随机数,etc组1、组2、组3
如果(!isset($counts[$rn])$counts[$rn]=0;//在第一次出现时初始化
如果($counts[$rn]<$max){
$tiimid[]=$rn;//将其添加到结果中
$counts[$rn]++;//…并调整计数
$trys=0;//重置安全设置
}
}
返回$tiimid;
}

while($rnUus=$rn)
替换为
while(countInArray($tiimid,$rnUus)=$max)
@IlyaBursov它不起作用,结果还是一样。另外,我忘了说我已经试过了。那么countInArray中有错误
function jagaTiimid($max, $liiget, $tArvLength, $tArv){
    $tiimid = []; //Starting array
    $counts = []; // Helper for quick count
    $tries = 0; // Counter to avoid infinite looping
    while (count($tiimid) < $liiget && $tries++ < 100) {
        $numbers = [];
        $rn = randomNumber($tArvLength, $tArv, $numbers); //Generate a random number for a group, etc group 1, group 2, group 3 
        if (!isset($counts[$rn])) $counts[$rn] = 0; // initialise on first occurence
        if ($counts[$rn] < $max) {
            $tiimid[] = $rn; // add it to the result
            $counts[$rn]++; // ... and adjust the count
            $tries = 0; // reset the safety
        }
    }
    return $tiimid;    
}