0
I have this function that is not working the in_array as it should
function genNumeros($min, $max, $quantity, $qtd, $somamin = false, $somamax = false)
{
for ($i = 0; $i <= $qtd; $i++) {
$numbers = range($min, $max);
shuffle($numbers);
$a = array_slice($numbers, 0, $quantity);
asort($a);
$x = array(14, 17);
if (in_array($x, $a)) {
continue;
}
if ($somamin) {
if (array_sum($a) < $somamin)
continue;
}
if ($somamax) {
if (array_sum($a) > $somamax)
continue;
}
foreach ($a as $key => $o) {
if (end(array_keys($a)) == $key) {
$aux = '';
} else {
$aux = ' - ';
}
echo $o . $aux;
}
echo '<br />';
}
}
It’s only working if I use it that way:
if (in_array(14, $a)) {
continue;
}
Example:
<?= genNumbers(1, 25, 15, 100, 201, 201) ?>
This example keeps returning values with 14 and 17 (which were not to appear):
- 2 - 5 - 6 - 7 - 9 - 11 - 13 - 14 - 16 - 17 - 18 - 19 - 21 - 22 - 25
- 1 - 3 - 6 - 7 - 8 - 11 - 13 - 15 - 17 - 18 - 20 - 21 - 22 - 23 - 25
- 1 - 3 - 5 - 6 - 8 - 11 - 12 - 15 - 17 - 19 - 20 - 21 - 22 - 24 - 25
- 2 - 3 - 5 - 8 - 10 - 12 - 15 - 16 - 18 - 19 - 20 - 22 - 23 - 24 - 25
- 3 - 4 - 7 - 8 - 9 - 10 - 13 - 14 - 15 - 17 - 20 - 21 - 23 - 24 - 25
What’s wrong with it?
You’re trying to locate array( 14, 17), not 14 and 17 separately. That’s not what you’re looking for?
if ( in_array(14, $a) || in_array( 17, $a ) ) {
 continue;
 }
– Bacco
So I know it works, I had already tested it here. But it’s not scalable that way. If I need 5 numbers? 10? And I’ll use that in function somehow, like
genNumbers(1, 25, 15, 100, 201, 201, array(14, 17, 5, 2))
(something like that). That’s why I’m in this dilemma :– Thiago
@Hiago you are developing this genNumbers for lottery games?
– Gabriel Rodrigues
@Gabrielrodrigues I am, because? ;)
– Thiago
@Thiago da uma olhada neste minha pergunta :) http://answall.com/questions/46248/como-melhoraro-processo-de-gera%C3%A7%C3%A3o-de-n%C3%Bameros-aleat%C3%B3rios-n%C3%A3o-repeats
– Gabriel Rodrigues