Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.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/0/email/3.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 - Fatal编程技术网

Php 循环功能,直到干草堆中没有指定的针为止

Php 循环功能,直到干草堆中没有指定的针为止,php,Php,我正在尝试构建一个函数,它将检查指针是否在干草堆中,如果在干草堆中,那么函数应该循环直到没有指针为止 到目前为止,我已经建立了一个生成随机数的函数。该函数的作用是创建一个随机字符串,然后用sha512对其进行散列。然后,该函数从字符串中提取所有数字并将其乘以2 它留给我一长行数字,然后我将使用mt_rand来指定strpo的起点和终点。之后,我只返回随机创建的数字 function randomNumber($suurus, $max){ mb_internal_encoding("UTF-8"

我正在尝试构建一个函数,它将检查指针是否在干草堆中,如果在干草堆中,那么函数应该循环直到没有指针为止

到目前为止,我已经建立了一个生成随机数的函数。该函数的作用是创建一个随机字符串,然后用sha512对其进行散列。然后,该函数从字符串中提取所有数字并将其乘以2

它留给我一长行数字,然后我将使用mt_rand来指定strpo的起点和终点。之后,我只返回随机创建的数字

function randomNumber($suurus, $max){
mb_internal_encoding("UTF-8");
$possible="aábcdeéfghiíjklmnoóöópqrstuúüűvwxyzAÁBCDEÉFGHIÍJKLMNOÓÖŐPQRSTUÚVWXYZ";
$char = mb_substr($possible,rand(0, mb_strlen($possible) - 1),1);
$str = openssl_digest($char, 'sha512');
$str = preg_replace('/[^1-9.]+/', '', $str);
$str = $str*2;
$strLen = strlen($str)-5;
$strStart =  mt_rand(1, $strLen);
$strEnd = mt_rand(1, $suurus);
$str = substr($str, $strStart, $strEnd);
while($str > $max){
  $str = $str / 2;
}
return round($str);
问题是这个数字不能重复,我需要它在我的应用程序中始终是唯一的

实际上,我用if-else语句完成了我想要的,但这意味着我必须编写一个长脚本,重复if语句,直到得到我想要的结果。我真的不认为这是最聪明的做事方式

if(!isset($voitjad)){
                      $voitjad[$vCounter][] = array(
                        'nimi' => $voitja,
                        'auhind' => $keyCode['v'.$vCount.''],
                        'rn' => $rN,
                        'siin' => 1,
                        'id' => $voitjaID
                      );
                    }else{
                      if(!exist($rN, $voitjad)){
                        $voitjad[$vCounter][] = array(
                          'nimi' => $voitja,
                          'auhind' => $keyCode['v'.$vCount.''],
                          'rn' => $rN,
                          'siin' => 1,
                          'id' => $voitjaID
                        );
                        }else{
                          $rN = randomNumber($atLength, $atCount);
                          $voitja = $attendcheckfb['attending'][$rN]['name'];
                          $voitjaID = $attendcheckfb['attending'][$rN]['id'];
                          if(!exist($rN, $voitjad)){
                            $voitjad[$vCounter][] = array(
                              'nimi' => $voitja,
                              'auhind' => $keyCode['v'.$vCount.''],
                              'rn' => $rN,
                              'siin' => 2,
                              'id' => $voitjaID
                            );
                          }else{ and so on....
我还尝试了do while循环,但我无法真正让它工作,我不确定我到底做错了什么。如果你能教育我,用信息射我

 $nimed = array_values($nimed);
                      $voitja = $nimed[$rN]['name'];
                      $voitjaID = $nimed[$rN]['id'];
                      $voitjad[$vCounter][] = array(
                        'nimi' => $voitja,
                        'auhind' => $keyCode['v'.$vCount.''],
                        'rn' => $rN,
                        'siin' => 1,
                        'id' => $voitjaID
                      );
                      $oitjaCount++;
                      $rN = randomNumber($nimedLength, $nimedCount);
                    } while(!exist($rN, $voitjad));

如果有什么方法可以让我得到更好的随机生成的数字,那么请告诉我,我愿意接受建议。与此同时,我需要帮助处理这个针头,干草堆的事情

您可以通过循环来完成:

function get_unique_hashcode() {
    global $hash_array;
    do {
        $hashcode = make_random_hashcode();
    } while (in_array($hashcode, $hash_array));
    $hash_array[] = $hashcode;
    return $hashcode;
}
或者您可以使用递归来实现:

function get_unique_hashcode() {
    global $hash_array;
    $hashcode = make_random_hashcode();
    if (in_array($hashcode, $hash_array)) {
        return get_unique_hashcode();
    } else {
        $hash_array[] = $hashcode;
        return $hashcode;
    }
}

这只是一个一般结构,您可能需要根据具体需要对其进行调整。

使用递归函数。好的,我将用谷歌搜索它是什么。@drpzxc一个调用自身的函数。所以,如果没有得到期望的结果,只需重新调用该函数。@Daniel,这正是我想要的。有趣的是,我在搜索网络时找不到它。
do while
循环应该可以工作
do{make new$needle;}while(在_数组中($needle,$haystack))
我不能很好地测试这个,知道吗,它似乎应该可以工作,但我稍后会在测试之后进行投票。我现在明白了为什么我的while循环不能像我预期的那样工作。我现在知道了递归函数,你们帮了我很大的忙。谢谢