Illogical in_array Behavior, not really...
Posted: Tue Oct 01, 2013 10:17 am
I wrote a program that extracts data from an image, and one of the things it does is build an array of colors used. I just came across an image that wasn't behaving well, and tracked it down to the following issue. Here is the relavent code:
and here is the output when it hits a "missing" (from the results) color:
[text]Checking Color: string(6) "0e2411"
Against: array(2) {
[0]=>
string(6) "0e2319"
[1]=>
string(6) "243b31"
}
EXISTS [/text]
There is no missing code or logic between the dump of the variables and the if statement that checks for the color in the array.
Now, if I go and change it to have a third parameter of TRUE (to enable STRICT matching) it works fine???
At first I'm thinking "WTF? they are all string(6), what difference does that make...
It finally dawned on me what the problem was... Can you figure it out?
Code: Select all
echo "Checking Color: ";
var_dump($strColor);
echo "Against: ";
var_dump($aryColors);
if (!in_array($strColor,$aryColors)) {
$aryColors[] = $strColor;
echo " ADDING \n";
} else {
echo " EXISTS \n";
}[text]Checking Color: string(6) "0e2411"
Against: array(2) {
[0]=>
string(6) "0e2319"
[1]=>
string(6) "243b31"
}
EXISTS [/text]
There is no missing code or logic between the dump of the variables and the if statement that checks for the color in the array.
Now, if I go and change it to have a third parameter of TRUE (to enable STRICT matching) it works fine???
At first I'm thinking "WTF? they are all string(6), what difference does that make...
It finally dawned on me what the problem was... Can you figure it out?