我有一个十六进制颜色代码数组,想要遍历它们并与数据库条目进行比较。这是代码:
// 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);
问题是通过循环的倒数第二次打印出一个null,最后一种颜色完全丢失。我已经尝试了几种不同的方式,但结果都是一样的。
这是输出:
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 )
我之前从未遇到过简单数组的问题......我没看到什么?我有一种可怕的感觉,这是一个愚蠢的东西,它是一个愚蠢的零宽度空间或类似的东西,但我找不到它...
我很感激帮助。
答案 0 :(得分:3)
此代码:
if(!in_array($row['color1'],$colors))
{
$colors[] = $row['color1'];
}
if(!in_array($row['color2'],$colors))
{
$colors[] = $row['color2'];
}
如果$row['color1']
或$row['color2']
为空/空,则会在colors数组中插入一个空白条目。将检查修改为这样的东西,这应该摆脱你的空值。
if( !empty( $row['color1']) && !in_array($row['color1'],$colors))
{
$colors[] = $row['color1'];
}
if( !empty( $row['color2']) && !in_array($row['color2'],$colors))
{
$colors[] = $row['color2'];
}
编辑:以下是复制循环输出的一种方法。
$row['color1']
为空,$row['color2']
为51A800
参见下面的演示,它完全复制了循环的输出。
我认为该条目为空白是错误的 - 数组中实际上不存在。检查上面的数组输出:
[71] => FF3939 [73] => 51A800 [74] => 8FC400 )
^^
????
如果您根据count( $array)
进行循环,则会得到73
,这是正确元素的数量。您从0-73
循环,而72
存在 NOT 。您永远不会得到74
,因为数组中只有73
个元素,所以您永远不会得到索引74
。弄清楚为什么索引72
丢失/消失,你将解决整个问题。也许真正的问题在于没有粘贴到SO的代码。
运行此代码,here is a demo复制输出(如果错误报告已关闭,则会发生),如果该行某处72
被删除。
最后,here is a demo复制输出完全,如果某些条目插入到数组中,然后在$row
颜色插入$colors
之前删除阵列。我最好的猜测是,这是OP省略代码的某些部分所发生的事情。
答案 1 :(得分:-1)
不应该是:
..$i<sizeof($count)