PDA

View Full Version : in_array problem.



shachi
08-19-2006, 04:58 PM
Hello,

I am simply trying to see if a value exists in an array of external file read by the file() function but unfortunately neither the in_array function is working nor the search_array. My code looks like this.



<?php
//$arr = array('red', 'green', 'blue');
$arr = file('colors.txt');
if(in_array('green',$arr)){
echo "Fount at index:".in_array('green',$arr);
} else {
echo "Not found";
}
?>


txt file, colors.txt



red
green
blue
green


It works with the arrays created with the array function but not with the file function any help would be appreciated. Thanks.

mwinter
08-19-2006, 07:25 PM
I am simply trying to see if a value exists in an array of external file read by the file() function but unfortunately neither the in_array function is working nor the search_array.

As stated in the manual, the array elements created by the file function including the new line characters that were present in the file. You haven't taken that into consideration.

Mike

shachi
08-20-2006, 02:15 PM
Thanks for your reply but I have already solved the problem.:p