简体   繁体   English

array_search / in_array找不到字符串

[英]array_search/in_array can't find string

I have the following code 我有以下代码

$checkarray = unserialize(file_get_contents('serialized.txt'));

var_dump($checkarray);
foreach($checkarray as $_index => $_image )
{
    echo strval($_index)." = ".var_dump($_image)."<br>";
}
var_dump(array_search('pirates_of_love_and_kingdoms.jpg',$checkarray));
var_dump(in_array('pirates_of_love_and_kingdoms.jpg',$checkarray));

the contents of 'serialized.txt' can be found here ( http://textuploader.com link, not a download link, will need to copy and paste into new file if you want to use it) 可以在此处找到“ serialized.txt”的内容( http://textuploader.com链接,而不是下载链接,如果要使用它,则需要复制并粘贴到新文件中)

the first var_dump output the array however because i have xdebug not the entire array is outputted, i'm not looking to get this fixed, it just confirms that the file was imported and unserialize correctly, the loop outputs everything in the array confirming that every value is a string (thanks to xdebug), the final 2 var_dumps are to output the results of the functions. 第一个var_dump输出数组,但是因为我有xdebug而不是输出整个数组,所以我不想解决这个问题,它只是确认文件已导入并正确反序列化,循环输出数组中的所有内容,从而确认每个value是一个字符串(由于xdebug),最后2个var_dumps将输出函数的结果。

when i run my code, both var_dump s output false, however if i use the browser to search for the text i do find it so i know it's in the array. 当我运行我的代码时,两个var_dump的输出都为false,但是,如果我使用浏览器搜索文本,我会找到它,因此我知道它在数组中。

I know that array_search returns the key in the array if the needle is found while in_array returns true if the needle is found and both will return false if the needle can't be found, however i do not get how neither can find it when i can confirm it's outputted in the loop and in my serialized.txt file at the same index as what is specified in the file. 我知道array_search如果找到了针头则返回数组中的键,而in_array如果找到针头则返回true,并且如果找不到针头都将返回false,但是我不知道怎么回事。可以确认它在循环中以及在我的serialized.txt文件中的输出与文件中指定的索引相同。

i have already checked the basics, white spaces, new lines, casing in both what is outputted on the screen and in the file, can anyone explain to me what i have done wrong? 我已经检查了屏幕和文件输出的基本信息,空格,换行和大小写,任何人都可以向我解释我做错了什么吗?

The line breaks at the end of each line are causing problems for you. 每行末尾的换行符为您造成问题。 Do a trim inside your foreach : 在您的foreach进行trim

$checkarray = unserialize(file_get_contents('serialized.txt'));

var_dump($checkarray);
foreach($checkarray as $_index => $_image )
{
    $checkarray[$_index] = trim($_image);
    echo strval($_index)." = ".var_dump($_image)."<br>";
}
var_dump(array_search('pirates_of_love_and_kingdoms.jpg',$checkarray));
var_dump(in_array('pirates_of_love_and_kingdoms.jpg',$checkarray));

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

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