-1

I am trying to generate a six character alphanumeric unique code using php. I need to generate 100000 unique codes and push into an array.

So far I have done this

$random_numbers = [];
function random_strings()
{
  $str_result ='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  while($i <=100000){
    array_push($random_numbers,substr(str_shuffle($str_result),0,6));
     $i++;
  }
 print_r($random_numbers);
 }

But my server dies when running this code. Do you have any idea, how it can be generated? Thank you

Shaunak Sontakke
  • 980
  • 1
  • 7
  • 17
Binita Gyawali
  • 256
  • 2
  • 13

4 Answers4

0

I fixed the problem. I was declaring the array outside the function

function random_strings() 
{
  $random_numbers = [];
  $str_result ='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  while($i <=100000){
    array_push($random_numbers,substr(str_shuffle($str_result),0,6));
    $i++;
  }
 print_r($random_numbers);
}
Binita Gyawali
  • 256
  • 2
  • 13
0

To ensure you get unique strings, you can use the random string as the key to the array instead, then loop till the array has the required number of entries. This code allows you to pass the number of values you want as well as it returns the array rather than just printing it...

function random_strings( $count )
{
    $random_numbers = [];
    $str_result ='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    while( count($random_numbers) < $count ){
        $random_numbers[substr(str_shuffle($str_result),0,6)] ='';
    }
    return array_keys($random_numbers);
}

print_r(random_strings(100000));
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Simple two line code for you

$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
echo substr(str_shuffle($permitted_chars), 0, 6)

Function example

$n is count of strings

$length is length of each string

function get_random_string($n, $length = 6){
    $res = [];
    for($i=0; $i<$n; $i++){
        $permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $res[] = substr(str_shuffle($permitted_chars), 0, $length);
    }
    return $res;
}

Final result

var_dump(get_random_string(10000, 6));

Result of function

array(10) {
  [0]=>
  string(6) "iIrekz"
  [1]=>
  string(6) "SG4Bki"
  [2]=>
  string(6) "5VBhMd"
  [3]=>
  string(6) "ubqcLr"
  [4]=>
  string(6) "hPUqng"
  [5]=>
  string(6) "9xfvpB"
  [6]=>
  string(6) "m3UQb6"
  [7]=>
  string(6) "E9nK3Z"
  [8]=>
  string(6) "1D0ob5"
  [9]=>
  string(6) "uC2koH"
  ...
}
Aaron Yordanyan
  • 738
  • 7
  • 19
0

Generating random numbers doesn't guarantee uniqueness. Since you want it for coupon codes, you want it to be accurate.

function random_strings() {
    $random_numbers = [];
    while( count( $random_numbers ) != 100000 ) {
        $uniqueString                          = md5( microtime() );
        $requiredLengthString                  = strtoupper( substr( $uniqueString, 0, 6 ) ); // substring would not be unique
        $random_numbers[$requiredLengthString] = NULL; // Putting $requiredLengthString as key so same keys would be overwritten, guaranteeing uniqueness.
    }

    return array_keys( $random_numbers );
}

print_r( random_strings() );

Inline refactoring if you wish to reduce LOC:

function random_strings() {
    $random_numbers = [];
    while( count( $random_numbers ) != 100000 ) {
        $random_numbers[strtoupper( substr( md5( microtime() ), 0, 6 ) )] = NULL; // Putting this string as key so same keys would be overwritten, guaranteeing uniqueness.
    }

    return array_keys( $random_numbers );
}
print_r( random_strings() );
Shaunak Sontakke
  • 980
  • 1
  • 7
  • 17