array not going through php function in_array

Multi tool use
up vote
1
down vote
favorite
I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.
$words = file('stopWords.txt');
//var_dump($words);
When I try something like:
if( in_array('able', $words) ) {
echo "match found";
}
Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.
php arrays function
|
show 1 more comment
up vote
1
down vote
favorite
I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.
$words = file('stopWords.txt');
//var_dump($words);
When I try something like:
if( in_array('able', $words) ) {
echo "match found";
}
Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.
php arrays function
can you provide an example ofvar_dump($words);
output?
– Ataur Rahman
2 days ago
3
FILE_IGNORE_NEW_LINES
- AKA Omit newline at the end of each array element.able
!=ablen
Or in other words$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago
the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago
@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago
It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.
$words = file('stopWords.txt');
//var_dump($words);
When I try something like:
if( in_array('able', $words) ) {
echo "match found";
}
Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.
php arrays function
I inserted, using the file(), words from a .txt file into an array. According to var_dump() the values are strings, and print_r give me a normal one dimension array.
$words = file('stopWords.txt');
//var_dump($words);
When I try something like:
if( in_array('able', $words) ) {
echo "match found";
}
Nothing happens, I've tried making a test array with and it works fine.
The array that $word outputs has 636 elements in it. Maybe that has something to do? Although I doubt it because I've tried it with a larger array and it still worked. I'm not sure what's causing this to happen, it seems to only have problems with this specific array. Can someone please help me out here, thanks.
php arrays function
php arrays function
asked 2 days ago
Brian hernandez
226
226
can you provide an example ofvar_dump($words);
output?
– Ataur Rahman
2 days ago
3
FILE_IGNORE_NEW_LINES
- AKA Omit newline at the end of each array element.able
!=ablen
Or in other words$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago
the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago
@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago
It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago
|
show 1 more comment
can you provide an example ofvar_dump($words);
output?
– Ataur Rahman
2 days ago
3
FILE_IGNORE_NEW_LINES
- AKA Omit newline at the end of each array element.able
!=ablen
Or in other words$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago
the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago
@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago
It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago
can you provide an example of
var_dump($words);
output?– Ataur Rahman
2 days ago
can you provide an example of
var_dump($words);
output?– Ataur Rahman
2 days ago
3
3
FILE_IGNORE_NEW_LINES
- AKA Omit newline at the end of each array element. able
!= ablen
Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago
FILE_IGNORE_NEW_LINES
- AKA Omit newline at the end of each array element. able
!= ablen
Or in other words $words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago
the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago
the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago
@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago
@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago
It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago
It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Instead of just
$words = file('stopWords.txt');
Use
$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
flags
The optional parameter flags can be one, or more, of the following constants:
FILE_USE_INCLUDE_PATH
Search for the file in the include_path.
FILE_IGNORE_NEW_LINES
Omit newline at the end of each array element
FILE_SKIP_EMPTY_LINES
Skip empty lines
This is the most relevant one Omit newline at the end of each array element
.
Basically this is what you are doing:
if( in_array('able', ["ablen"]) ) {
echo "match found";
}
Which is false
. Or in other words its not exactly equivalent to doing explode("n", $contents)
as the line (array items) still contain the newline. Which because in_array
is not a fuzzy search (basically its ==
) it won't match them 'able' != 'ablen'
.
Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.
http://php.net/manual/en/function.file.php
You could also trim the items in the array like this $words=array_map('trim', $words);
but why bother when there is a flag for it.
Cheers
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Instead of just
$words = file('stopWords.txt');
Use
$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
flags
The optional parameter flags can be one, or more, of the following constants:
FILE_USE_INCLUDE_PATH
Search for the file in the include_path.
FILE_IGNORE_NEW_LINES
Omit newline at the end of each array element
FILE_SKIP_EMPTY_LINES
Skip empty lines
This is the most relevant one Omit newline at the end of each array element
.
Basically this is what you are doing:
if( in_array('able', ["ablen"]) ) {
echo "match found";
}
Which is false
. Or in other words its not exactly equivalent to doing explode("n", $contents)
as the line (array items) still contain the newline. Which because in_array
is not a fuzzy search (basically its ==
) it won't match them 'able' != 'ablen'
.
Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.
http://php.net/manual/en/function.file.php
You could also trim the items in the array like this $words=array_map('trim', $words);
but why bother when there is a flag for it.
Cheers
add a comment |
up vote
1
down vote
accepted
Instead of just
$words = file('stopWords.txt');
Use
$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
flags
The optional parameter flags can be one, or more, of the following constants:
FILE_USE_INCLUDE_PATH
Search for the file in the include_path.
FILE_IGNORE_NEW_LINES
Omit newline at the end of each array element
FILE_SKIP_EMPTY_LINES
Skip empty lines
This is the most relevant one Omit newline at the end of each array element
.
Basically this is what you are doing:
if( in_array('able', ["ablen"]) ) {
echo "match found";
}
Which is false
. Or in other words its not exactly equivalent to doing explode("n", $contents)
as the line (array items) still contain the newline. Which because in_array
is not a fuzzy search (basically its ==
) it won't match them 'able' != 'ablen'
.
Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.
http://php.net/manual/en/function.file.php
You could also trim the items in the array like this $words=array_map('trim', $words);
but why bother when there is a flag for it.
Cheers
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Instead of just
$words = file('stopWords.txt');
Use
$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
flags
The optional parameter flags can be one, or more, of the following constants:
FILE_USE_INCLUDE_PATH
Search for the file in the include_path.
FILE_IGNORE_NEW_LINES
Omit newline at the end of each array element
FILE_SKIP_EMPTY_LINES
Skip empty lines
This is the most relevant one Omit newline at the end of each array element
.
Basically this is what you are doing:
if( in_array('able', ["ablen"]) ) {
echo "match found";
}
Which is false
. Or in other words its not exactly equivalent to doing explode("n", $contents)
as the line (array items) still contain the newline. Which because in_array
is not a fuzzy search (basically its ==
) it won't match them 'able' != 'ablen'
.
Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.
http://php.net/manual/en/function.file.php
You could also trim the items in the array like this $words=array_map('trim', $words);
but why bother when there is a flag for it.
Cheers
Instead of just
$words = file('stopWords.txt');
Use
$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
flags
The optional parameter flags can be one, or more, of the following constants:
FILE_USE_INCLUDE_PATH
Search for the file in the include_path.
FILE_IGNORE_NEW_LINES
Omit newline at the end of each array element
FILE_SKIP_EMPTY_LINES
Skip empty lines
This is the most relevant one Omit newline at the end of each array element
.
Basically this is what you are doing:
if( in_array('able', ["ablen"]) ) {
echo "match found";
}
Which is false
. Or in other words its not exactly equivalent to doing explode("n", $contents)
as the line (array items) still contain the newline. Which because in_array
is not a fuzzy search (basically its ==
) it won't match them 'able' != 'ablen'
.
Note: Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.
http://php.net/manual/en/function.file.php
You could also trim the items in the array like this $words=array_map('trim', $words);
but why bother when there is a flag for it.
Cheers
edited 2 days ago
answered 2 days ago


ArtisticPhoenix
14.6k11223
14.6k11223
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53184053%2farray-not-going-through-php-function-in-array%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
AKU,XbHB1nYsZzuEXNH6haetaLk3,cTv3 N7Vsopha ZivM KRUgo,VJBhsdv YlOEKkfl5n cf pscLj,R dynyFZ5bngvzn
can you provide an example of
var_dump($words);
output?– Ataur Rahman
2 days ago
3
FILE_IGNORE_NEW_LINES
- AKA Omit newline at the end of each array element.able
!=ablen
Or in other words$words = file('stopWords.txt',FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
– ArtisticPhoenix
2 days ago
the array is really big so var_dump outputs a lot, do you wants just a snippet?
– Brian hernandez
2 days ago
@ArtisticPhoenix how and why did this work?! Thank you so much
– Brian hernandez
2 days ago
It's magic. It's because the return has line endings, and in array is not fuzzy.
– ArtisticPhoenix
2 days ago