1
Good evening guys, I was developing a script in php and when I put true being passed by parameter to a function inside an array dynamically , he gave a bug.
I can not explain the bug right, but the function works almost equal to array_unique
. if placed true
he acts like the array_unique
, if false he simply repeats what was passed to him.
Follow the code below:
function random_caractere_v2($veredito) {
if($veredito) {
$elemento = func_get_args();
$array = array();
foreach ($elemento as $indice => $valor) {
if(in_array($valor, $array)) {
continue;
}
if(!in_array("$valor", $array)) {
$array[] = $valor;
}
}
print_r($array);
}
if(!$veredito) {
$elemento = func_get_args();
print_r($elemento);
}
}
random_caractere_v2(true,'lapiseira','colchao','caderno','quimica','quimica');
I was able to solve the problem using unset to take out the problem Indice, but I’m curious to find out why the bug.
And what’s the bug?
– Everton Neri
the bug is that it only passes the value 1 that would be true 1. the rest of the values n passes , the condition is that if the value is already in the array array array it jumps the assignment and goes to the low condition that is : if the value was not found in the array array then assign it, but the n code does so , gets stuck in the first condition , as if the value was already inside the vector
– relaie
really very strange.. switching ! in_array to ! isset here worked normal, I’ll give a search to understand the reason for this error
– Everton Neri
I was only able to solve , using the unset function in the vector Indice 0
– relaie
For me it is not clear what the function is supposed to do. It was preferable to give one or two examples of input and output values, to realize the purpose of the code and then go to solve the problem.
– Isac
with the true it will return [0] => pencil holder [1] =>square [2] =>notebook [3] => chemical
– relaie
with false it returns the same thing that was passed , without taking the repeated value
– relaie