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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.