Jump to content

Problem with in_array


litebearer

Recommended Posts

I can't seem to understand why in_array is NOT finding the 'needle'.

 

php file:

<?PHP
$search = "WS11";
$valid_codes = file("outcode2.txt");
if(in_array($search, $valid_codes)){
echo "yes";
}else{
echo "No";
}
echo $search . "<hr>";
echo "<PRE>";
print_r($valid_codes);
echo "</pre>";
?>

 

outcode2.txt:

WS10
WS11
WS12
WS13
WS14
WS15

 

WS11 is in the outcode2.txt

It is not an issue of case.

Perhaps my old eyes just keep missing the problem.

Any help is appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/262315-problem-with-in_array/
Share on other sites

My bet would be the "\n" or linebreak after each item. If i remember right, file does not trim them out.

 

$valid_codes = file("outcode2.txt");
array_walk($valid_codes, 'trim');

 

Should trim out the line breaks. Another way to do it is read in the file with file_get_contents and then use explode. If you want to find out if there really is an extra character, use var_dump on the $valid_codes and it should tell you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.