php in_array - 无法验证项目的存在

时间:2017-03-29 00:22:19

标签: php

我正在尝试检查一个项目是否存在于从文件填充的数组中,该文件由文件名组成,每行一个,如下所示:
GG.jpg
2J.jpg
WWWW.jpg

到目前为止,我有以下内容:

$UsedAvatars = array();

$UsedAvatars = file('UsedAvatars.txt',  FILE_IGNORE_NEW_LINES );

// This next part is to check that the array has been populated, and
// it works.

for($counter=0; $counter<count($UsedAvatars); $counter++)
{
    echo $counter . "\t" . $UsedAvatars[$counter] . "<br>";
}

echo "<br>";

    // This is hardwired in because I know that it is in the text file
    // used to populate the array and so should return TRUE
    $filename="GG.jpg";

if( in_array( $filename, $UsedAvatars ) )
{
  echo "found";
}

但是,测试不起作用,并且不输出“找到”。我看不出有什么问题,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

尝试替换

if( in_array( $filename, $UsedAvatars ) )
{
  echo "found";
}

$tmp = array_flip ($UsedAvatars);
if ($tmp[$filename] != "")
    echo "found";