简体   繁体   English

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

[英]php in_array - can't verify presence of an item

I'm trying to check whether an item exists in an array that is populated from a file, which consists of filenames, one on each line, like this: 我正在尝试检查从文件填充的数组中是否存在一项,该文件由文件名组成,每行一个,如下所示:
GG.jpg GG.jpg
2J.jpg 2J.jpg
WWWW.jpg WWWW.jpg

I have the following so far: 到目前为止,我有以下内容:

$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";
}

However, the test does not work and "found" is not outputted. 但是,该测试不起作用,并且不会输出“找到”。 I cannot see what is wrong, can anyone help please? 我看不出有什么问题,有人可以帮忙吗?

try to replace 尝试更换

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM