PHP简单数组神秘空值


PHP Simple Array Mystery Null Value

我有一个十六进制颜色代码数组,想循环遍历它们并与数据库条目进行比较。下面是代码:

// 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!!! ----&gt; '.$colors[$i].'<br>';
    else
        echo 'Nope, not this one... '.$colors[$i].'<br>';
}
echo "&lt;br&gt;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 ) 

我以前从来没有遇到过简单数组的问题…我没看到什么?我有一种可怕的感觉,这是其中一个愚蠢的东西,它是一个愚蠢的零宽度空格或类似的东西,但我找不到它…

谢谢你的帮助。

代码:

if(!in_array($row['color1'],$colors))
{
    $colors[] = $row['color1'];
}
if(!in_array($row['color2'],$colors))
{
    $colors[] = $row['color2'];
}
如果$row['color1']$row['color2']为空/空,

将在colors数组中插入一个空白条目。将check修改为如下所示,这样可以去掉空值。

if( !empty( $row['color1']) && !in_array($row['color1'],$colors))
{
    $colors[] = $row['color1'];
}
if( !empty( $row['color2']) &&  !in_array($row['color2'],$colors))
{
    $colors[] = $row['color2'];
}

Edit:这是复制循环输出的一种方法。

  1. 你把索引弄混了,74不存在。
  2. $row['color1']为空,$row['color2']51A800
  3. 您可能已经关闭了警告/错误报告

请参阅下面的演示,它完全复制了循环的输出。

演示

编辑:找到空白条目

我错误地认为条目是空白的-它实际上是不存在在数组中。查看上面的数组输出:

[71] => FF3939 [73] => 51A800 [74] => 8FC400 )
              ^^
             ????

如果基于count( $array)进行循环,则得到73,这是正确的元素数。从0-73开始循环,而72不存在NOT。你永远不会得到74,因为数组中只有73个元素,所以你永远不会得到索引74。找出索引72丢失/消失的原因,您将解决整个问题。也许真正的问题在于没有粘贴到SO的代码。

运行这个,这里是一个复制输出的演示(如果错误报告被关闭,它会),如果在行的某个地方,72被擦除。

最后,这里有一个演示,如果在将$row颜色插入到$colors数组之前将某些条目插入到数组中,然后将其删除,则该演示完全复制输出。我最好的猜测是,这就是OP省略代码的某些部分发生的事情。

应该是:

..$i<sizeof($count)