I have an array of hex color codes and want to loop through them and compare to a database entry. Here is the code:
// database connect & query hidden for brevity
$row = mysql_fetch_assoc($result);
$colors = array("EEEEEE","EEEED8","EEEEAA","EEEE9A","EEEE77","EEEE49","EEEE21","EEEE00",
"DAFFFF","DAFFF3","DAFFDA","DAFFD2","DAFFBF","DAFFA6","DAFF91","DAFF7F",
"9FFFFF","9FFFED","9FFFC8","9FFFBB","9FFF9F","9FFF7A","9FFF5A","9FFF3F",
"19FFFF","19FFEA","19FFBD","19FFAE","19FF8C","19FF60","19FF39","19FF19",
"BBC8FF","9FC8FF","7AC8FF","5AC8FF","3FC8FF","7A7AFF","5A7AFF","3F7AFF",
"BB7AFF","9F7AFF","608CFF","398CFF","198CFF","6039FF","3939FF","1939FF",
"EEAAAA","EEAAEE","EEAAD8","FF91D2","C87AFF","8C39FF","BD39FF","AE39FF",
"EEAA77","EEAA49","EEAA21","EEAA00","FF5A3F","FF6039","FF6019","FF3919",
"FF5A9F","FF5A7A","FF5A5A","FF6060","FF3960","FF3F5A","FF3F3F","FF3939");
if(!in_array($row['color1'],$colors))
{
$colors[] = $row['color1'];
}
if(!in_array($row['color2'],$colors))
{
$colors[] = $row['color2'];
}
$count = count($colors);
echo "The Database says Color1 is: ".$row['color1']."<br>";
echo "The Colors Array says it is: ".$colors[73]."<br><br>";
echo "The Database says Color2 is: ".$row['color2']."<br>";
echo "The Colors Array says it is: ".$colors[74]."<br><br><br>";
echo "Going Through the Loops shows:<br><br>";
for($i=0;$i<$count;$i++)
{
if(('#'.$colors[$i]) == $row['color1'])
echo 'FOUND IT!!! ----> '.$colors[$i].'<br>';
else
echo 'Nope, not this one... '.$colors[$i].'<br>';
}
echo "<br>And Printing the Array we see:<br><br><br>";
print_r($colors);
The problem is the second to last iteration through the loop prints a null and the last color is lost entirely. I've tried this several different ways and all result in the same.
Here is the output:
The Database says Color1 is: 51A800
The Colors Array says it is: 51A800
The Database says Color2 is: 8FC400
The Colors Array says it is: 8FC400
Going Through the Loops shows:
Nope, not this one... EEEEEE
Nope, not this one... EEEED8
Nope, not this one... EEEEAA
Nope, not this one... EEEE9A
Nope, not this one... EEEE77
Nope, not this one... EEEE49
Nope, not this one... EEEE21
Nope, not this one... EEEE00
Nope, not this one... DAFFFF
Nope, not this one... DAFFF3
Nope, not this one... DAFFDA
Nope, not this one... DAFFD2
Nope, not this one... DAFFBF
Nope, not this one... DAFFA6
Nope, not this one... DAFF91
Nope, not this one... DAFF7F
Nope, not this one... 9FFFFF
Nope, not this one... 9FFFED
Nope, not this one... 9FFFC8
Nope, not this one... 9FFFBB
Nope, not this one... 9FFF9F
Nope, not this one... 9FFF7A
Nope, not this one... 9FFF5A
Nope, not this one... 9FFF3F
Nope, not this one... 19FFFF
Nope, not this one... 19FFEA
Nope, not this one... 19FFBD
Nope, not this one... 19FFAE
Nope, not this one... 19FF8C
Nope, not this one... 19FF60
Nope, not this one... 19FF39
Nope, not this one... 19FF19
Nope, not this one... BBC8FF
Nope, not this one... 9FC8FF
Nope, not this one... 7AC8FF
Nope, not this one... 5AC8FF
Nope, not this one... 3FC8FF
Nope, not this one... 7A7AFF
Nope, not this one... 5A7AFF
Nope, not this one... 3F7AFF
Nope, not this one... BB7AFF
Nope, not this one... 9F7AFF
Nope, not this one... 608CFF
Nope, not this one... 398CFF
Nope, not this one... 198CFF
Nope, not this one... 6039FF
Nope, not this one... 3939FF
Nope, not this one... 1939FF
Nope, not this one... EEAAAA
Nope, not this one... EEAAEE
Nope, not this one... EEAAD8
Nope, not this one... FF91D2
Nope, not this one... C87AFF
Nope, not this one... 8C39FF
Nope, not this one... BD39FF
Nope, not this one... AE39FF
Nope, not this one... EEAA77
Nope, not this one... EEAA49
Nope, not this one... EEAA21
Nope, not this one... EEAA00
Nope, not this one... FF5A3F
Nope, not this one... FF6039
Nope, not this one... FF6019
Nope, not this one... FF3919
Nope, not this one... FF5A9F
Nope, not this one... FF5A7A
Nope, not this one... FF5A5A
Nope, not this one... FF6060
Nope, not this one... FF3960
Nope, not this one... FF3F5A
Nope, not this one... FF3F3F
Nope, not this one... FF3939
Nope, not this one...
Nope, not this one... 51A800
And Printing the Array we see:
Array ( [0] => EEEEEE [1] => EEEED8 [2] => EEEEAA [3] => EEEE9A [4] => EEEE77 [5] => EEEE49 [6] => EEEE21 [7] => EEEE00 [8] => DAFFFF [9] => DAFFF3 [10] => DAFFDA [11] => DAFFD2 [12] => DAFFBF [13] => DAFFA6 [14] => DAFF91 [15] => DAFF7F [16] => 9FFFFF [17] => 9FFFED [18] => 9FFFC8 [19] => 9FFFBB [20] => 9FFF9F [21] => 9FFF7A [22] => 9FFF5A [23] => 9FFF3F [24] => 19FFFF [25] => 19FFEA [26] => 19FFBD [27] => 19FFAE [28] => 19FF8C [29] => 19FF60 [30] => 19FF39 [31] => 19FF19 [32] => BBC8FF [33] => 9FC8FF [34] => 7AC8FF [35] => 5AC8FF [36] => 3FC8FF [37] => 7A7AFF [38] => 5A7AFF [39] => 3F7AFF [40] => BB7AFF [41] => 9F7AFF [42] => 608CFF [43] => 398CFF [44] => 198CFF [45] => 6039FF [46] => 3939FF [47] => 1939FF [48] => EEAAAA [49] => EEAAEE [50] => EEAAD8 [51] => FF91D2 [52] => C87AFF [53] => 8C39FF [54] => BD39FF [55] => AE39FF [56] => EEAA77 [57] => EEAA49 [58] => EEAA21 [59] => EEAA00 [60] => FF5A3F [61] => FF6039 [62] => FF6019 [63] => FF3919 [64] => FF5A9F [65] => FF5A7A [66] => FF5A5A [67] => FF6060 [68] => FF3960 [69] => FF3F5A [70] => FF3F3F [71] => FF3939 [73] => 51A800 [74] => 8FC400 )
I've never had a problem with a simple array before... What am I not seeing? I have a terrible feeling this is one of those stupid things where it's a stupid zero-width space or something stupid like that, but I can't find it...
I appreciate the help.
var_dump($colors[$i]);
in the loop – roselan Nov 14 '11 at 23:09$colors
with '#' which you don't have in$row
as far as I can see. This may or may not be a problem depending on how much this corresponds to the production code. – Fluff Nov 14 '11 at 23:12$i
to identify the values. – Fluff Nov 14 '11 at 23:14if(('#'.$colors[$i]) == $row['color1'])
, but it appears both $row and $colors have the save format, without the "#", so you can delete it from the "if"if($colors[$i] == $row['color1'])
– roselan Nov 15 '11 at 0:16