Good evening guys, I was developing a script in php and when I put the true being passed by parameter to a function into an array dynamically, it gave a bug.
I can not explain the right bug, but the function works pretty much like array_unique
. if set true
it acts as array_unique
, if false it simply repeats what was passed to it.
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 managed to solve the problem using unset to get the index that caused the problem, but I'm curious to find out why the bug.