2

I have made a random character generator, but how do I check if an result is duplicate?
E.g., if I want 100000 results, how do I check if an result is not duplicate in the 100000 results?

for ($i=0; $i<$quantity; $i++){
$a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
$code = "$a-$b-$c-$d";
echo $code .'<br>';
}

Thanks in advance!

EdChum
  • 376,765
  • 198
  • 813
  • 562
mishad050
  • 448
  • 5
  • 15

4 Answers4

1

The first thing you'll want to do is save all the codes you generated into an array.

$codes = array();
for ($i=0; $i<$quantity; $i++){
    $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
    $code = "$a-$b-$c-$d";
    echo $code .'<br>';
    $codes[] = $code;
}

To check if it contains duplicates, this answer proposes the following solution:

if (count($codes) == count(array_unique($codes))) {
    // array contains duplicates
}

If you're interested in which code exactly is a duplicate, try this instead. I'm sorting the array first (to make sure equal values are consecutive), and then checking if two consecutive values are equal.

sort($codes);
for ($i = 0; $i < count($codes) - 2; $i++) {
    if ($codes[$i] == $codes[$i + 1]) {
        // $codes[$i] is a duplicate
    }
}

And if you're implying you want to get rid of duplicates, then it's as simple as using array_unique:

$codes = array_unique($codes);
Community
  • 1
  • 1
vvye
  • 1,208
  • 1
  • 10
  • 25
1

Something like this maybe? Using in_array()

$previous = array();

for ($i=0; $i<$quantity; $i++){
    $unique_found = false;
    while(!$unique_found){
        $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
        $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);

        $code = "$a-$b-$c-$d";

        if(!in_array($code,$previous)){
            $unique_found = true;
            $previous[] = $code;
            echo $code .'<br>';
        }
    }

}
CT14.IT
  • 1,635
  • 1
  • 15
  • 31
0
$myValues = array();
for ($i=0; $i<$quantity; (if($unique){$i++;})){
 $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
 $code = "$a-$b-$c-$d";
 $unique = false;
 if(!in_array($code,$myValues)){
   $myValues[] = $code;
   echo $code .'<br>';
   $unique = true;
 }
}
winston86
  • 159
  • 1
  • 8
0

You can use the code as key in an array, so repeated ones will be overwritten:

$results = array();
$quantity = 10000;
    while (count($results) < $quantity) {
       $a = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
       $b = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
       $c = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);
       $d = substr(str_shuffle("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 4);

       $code = "$a-$b-$c-$d";
       $results[$code] = $code;

    }
Carlos M. Meyer
  • 436
  • 3
  • 9