0

php array-search returns a blank

In the following code: echo $key returns a blank but echo $text[2] returns the word at that index. Can someone explain why is array-search not working here? It does work when I test it with a key => value type array. <?php $myfile = fopen("test.txt", "r") or die("Unable to open file!"); while(!feof($myfile)) { $text[] = fgets($myfile); } fclose($myfile); $word = $_POST['word']; $key = array_search($word, $text); echo $key ?>

2nd May 2020, 11:17 PM
Bill
2 Answers
+ 1
where is the form you are using for this?
3rd May 2020, 6:49 AM
Bahha┣
Bahha┣ - avatar
0
Thanks for your response. The problem was in the array. The entries all included \n from the test.txt file.
3rd May 2020, 9:53 PM
Bill