litebearer Posted May 9, 2012 Share Posted May 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262315-problem-with-in_array/ Share on other sites More sharing options...
premiso Posted May 9, 2012 Share Posted May 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262315-problem-with-in_array/#findComment-1344290 Share on other sites More sharing options...
litebearer Posted May 9, 2012 Author Share Posted May 9, 2012 Thank you! That is/was the problem. I had always presumed that file() returned an array using the newline as the delimiter (and did NOT include the newline as part of the array elements). Thank you again!!! Quote Link to comment https://forums.phpfreaks.com/topic/262315-problem-with-in_array/#findComment-1344295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.