I have a loop:
for ($p=1; $p<=$ncarts; $p++) {
$contador_pontos_cartela[$p] = 0;
for ($g=1; $g<=$quantidade_sorteada; $g++) {
foreach ($numeros_cartela[$p] as $w) {
echo "Valor de w: {$w} <br>";
echo "Valor de sorted: {$sorted[$g]} <br>";
if ($w == $sorted[$g]){
echo "truue";
$contador_pontos_cartela[$p] += 1;
}
}
}
}
And
$ncarts = 3;
$quantidade_sorteada = 3;
$numeros_cartela[1] = array(1, 3);
$numeros_cartela[2] = array (5, 7);
$numeros_cartela[3] = array(9, 11);
$sorted[1] = 1;
$sorted[2] = 3;
$sorted[3] = 5; `
I inserted that echo $w
and echo $sorted[$g]
to see if them values were correct. And the aswer is; YES. The output of that echo
is:
Valor de w: 1
Valor de sorted: 1
Valor de w: 3
Valor de sorted: 1
Valor de w: 1
Valor de sorted: 3
Valor de w: 3
Valor de sorted: 3
Valor de w: 1
Valor de sorted: 5
Valor de w: 3
Valor de sorted: 5
Valor de w: 5
Valor de sorted: 1
Valor de w: 7
Valor de sorted: 1
Valor de w: 5
Valor de sorted: 3
Valor de w: 7
Valor de sorted: 3
Valor de w: 5
Valor de sorted: 5
Valor de w: 7
Valor de sorted: 5
Valor de w: 9
Valor de sorted: 1
Valor de w: 11
Valor de sorted: 1
Valor de w: 9
Valor de sorted: 3
Valor de w: 11
Valor de sorted: 3
Valor de w: 9
Valor de sorted: 5
Valor de w: 11
Valor de sorted: 5
In other words, I have 3 positive results. Where $w
is equal $sorted
(on the 1,1; 3,3; and 5,5) But if ($w == $sorted[$g])
never becomes true... You know, I have a echo inside if
; echo "truue";
and this never is printed.
Can anybody see the problem?
The output of your var_dump():-
string(3) "1 " string(1) "1" string(3) "3 " string(1) "1" string(3) "1 " string(1) "3" string(3) "3 " string(1) "3" string(3) "1 " string(1) "5" string(3) "3 " string(1) "5" string(3) "5 " string(1) "1" string(3) "7 " string(1) "1" string(3) "5 " string(1) "3" string(3) "7 " string(1) "3" string(3) "5 " string(1) "5" string(3) "7 " string(1) "5" string(3) "9 " string(1) "1" string(4) "11 " string(1) "1" string(3) "9 " string(1) "3" string(4) "11 " string(1) "3" string(3) "9 " string(1) "5" string(4) "11 " string(1) "5"
Shows that you are not getting clean data/input. You are trying to compare "1 " with "1". Take a look at where these values come from and ensure that they are cleaned up. eg use trim()
to remove any unwanted white space or cast them to integers.
This problem often occurs when using data that has originally come from user input and has not been properly filtered.
I would recommend you do that with your source data, but as a quick fix, you could do it in your loop using trim()
.