There is uniqid to create a unique group of characters, but this function uses letters and numbers. And I already researched, but I did not find anything for just numbers. Anyone know any way to accomplish this?
There is uniqid to create a unique group of characters, but this function uses letters and numbers. And I already researched, but I did not find anything for just numbers. Anyone know any way to accomplish this?
It all depends on what you mean by "unique" and what you will do with those numbers. In principle, uniqid () returns values in hex that you can convert to decimals:
$uid = hexdec(uniqid());
Another solution would be this:
$digitos = '0123456789';
$tamanho = 16; // por exemplo...
$uid = '';
for ($i = 0; $i < $tamanho; $i++) {
$uid .= $digitos[rand(0, strlen($digitos) - 1)];
}
(Solution based in this code )
The result would be a random numeric string with 16 digits, for example.
You can use microtime
microtime - Returns a Unix timestamp with microseconds
Depending on the purpose can be a simple and viable alternative and with low risk of collision.
echo microtime();
0.97959400 1407273578
You can remove the. and space
Just go including the numbers in an array, checking first if it does not already exist in it.
$sorteados = array();
for($i = 0; $i <= 1000; ++$i) {
do {
$nr = rand(1000,10000);
} while (in_array($nr,$sorteados));
$sorteados[] = $nr;
}
print_r($sorteados);