Function with in_array not working

0

I have this function that in_array is not working 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 this way:

    if (in_array(14, $a)) {
       continue;
    }

Example:

<?= genNumbers(1, 25, 15, 100, 201, 201) ?>

This example continues to return 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
  • 3 - 4 - 7 - 8 - 9 - 10 - 13 - 14 - 15 - 17 - 20 - 21 - 23 -

What's wrong?

    
asked by anonymous 04.12.2015 / 02:54

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 exists within $array2 . I did not quite understand your logic in the question, but I believe that if it's not exactly what you want then it's 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 such a function then you would only swap a line in your code, where you use in_array to call 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. Shot on Fiddle the 14th and 17th do not appear.

    
04.12.2015 / 05:32
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', '...')) . Notice that you are passing the array in the first parameter: $x = array(14, 17); . In your logic, if you want to look inside your array $x each element of array $a , you should test each position: $x[0] and $x[1] for your array $a : in_array($x[0], $a) and in_array($x[1], $a) .

So you 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 do 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;
}
    
07.12.2015 / 18:07