I'm using Laravel 5.1 framework.
I need to create a set of uniques codes from characters 'BCDFGHIJKLMNPQRSTVWXYZ' take 5, for example DFGHJ.
So, my code is this:
$maxCodes = 1200000;
$codeSize = 5;
$characters = 'BCDFGHJKLMNPQRSTVWXY';
$cLenght = strlen($characters);
$sizeBlock = $maxCodes/($cLenght*4); // For not overflow memory when put the codes in array
for ($i=0; $i < $cLenght; $i++) {
$subCharacters = str_replace($characters[$i], '', $characters);
$existCodes = [];
for ($k = 0; $k < 4; $k++) {
$codesToInsert = [];
for ($j = 0; $j < $sizeBlock;) {
$code = $characters[$i] . substr(str_shuffle($characters), 0, $codeSize-1);
if (!isset($existCodes[$code])) {
$existCodes[$code] = '';
array_push($codesToInsert, ['code' => $code, 'used' => 0, 'rand' => rand(0,10)]);
$j++;
}
}
\App\Code::insert($codesToInsert);
}
}
I'll be explain a little my code. Because my codes are in alphabetic order, I put a field rand with a random number (I create this field for next make a query like Code::where('used', 0)->where('rand', rand(0,9)), by this way I retrieve a random code, not in alphabetic order)
The problem is when I run the function to generate the codes, its create only 135000 not $maxCodes, but I dont know why?..
Someone can help me?
Thanks!
PS.- Sorry for me english..