循环功能,直到大海捞针中没有指定的针

I'm trying to build a function, that will check if the needle is in haystack, if it is then the function should loop until there is none.

So far I've built a function for random number generation. What that function does is it will create a random string then hash it with sha512. The function then extracts all the numbers from a string and multiplies it by 2.

It leaves me with a long line of numbers, then I'll use mt_rand to specify start and end point for strpos. After that I just return the number that was created randomly.

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);

The thing is that this number can't repeat, I need it to be unique at all times for my app.

I actually accomplished what I wanted with if else statements, but that means I have to write a long script of repeated if statements until I get the results I want. I really don't think this is the smartest way to do things.

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....

I also tried with do-while loop, but I couldn't really get it working, I'm not sure what exactly I'm doing wrong here. If you can educate me, shoot me with information.

 $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));

If there's a way for me to get better randomly generated numbers, then please tell me, I'm open for suggestions. Meanwhile I need help with this needle, haystack thing.

You can either do it with a loop:

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

Or you can do it with recursion:

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;
    }
}

This is just a general structure, you may need to adjust it for your detailed needs.