Function with in_array is not working

Asked

Viewed 213 times

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 ) ) {&#xA; continue;&#xA; }

  • 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 :

  • @Hiago you are developing this genNumbers for lottery games?

  • @Gabrielrodrigues I am, because? ;)

  • @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

2 answers

0

The in_array function does not expect to receive two arrays, you can create a function to do this check.

This function below returns true if any content of $array1 exist within $array2. I do not understand your logic in the question, but I believe that if that is not exactly what you want then it is a matter of adapting little to the desired result.

function VerificaConteudoArray($array1, $array2) {
    foreach ($array1 as $inner) {
        if (in_array($inner,$array2)){
            return true;
        }
    }
}

Doing a function like this then you would only exchange a line in your code, where you use the in_array calls the new function. It would look like this:

<?php


genNumeros(10,20,5,6);


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 (VerificaConteudoArray($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 />';
    }
}



function VerificaConteudoArray($array1, $array2) {

    foreach ($array1 as $inner) {
        if (in_array($inner,$array2)){
            return true;
        }
    }
}


?>

Here’s a Fiddle with this code running. Rotated in Fiddle the 14 and 17 do not appear.

0

The first parameter must be the value to be searched for, and the second parameter is the array itself. in_array('valor', array('valor', 'valor1', '...')). See that you are passing the array in the first parameter: $x = array(14, 17);. In your logic, if to search inside your array $x each element of the array $a, you should test every position: $x[0] and $x[1] for your array $a: in_array($x[0], $a) and in_array($x[1], $a).

That should solve the problem:

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[0], $a) && in_array($x[1], $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 />';
    }
}

But if you want to make these dynamic values, I recommend that you create a method for this:

 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 (continueInArray($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 />';
        }
    }

function continueInArray($a, $b) {
    if (is_array($a)) {
        if (count($a)) {
           foreach ($a as $value) {
              return in_array($value, $b);
           }
        }
    } else {
       return in_array($a, $b);
    }
    return false;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.