Jump to content

Can't check in_array from file?


Rifts

Recommended Posts

Hey guys,

 

I have this super simple code which will not work and I'm very confused as to why.

 

Here is the code

 

// read the file into an array called $users
$users= file('users.txt');

if (in_array("joe", $users)) {
    echo "Got Joe";
}

 

When I print_r($users) everyone looks fine like this but it never returns true, Why?

 

Array
(
    [0] => joe

    [1] => bob

    [2] => mike

    [3] => ted

    [4] => fred
)

Link to comment
https://forums.phpfreaks.com/topic/259649-cant-check-in_array-from-file/
Share on other sites

Something a little extra.

 

<?php
$username = 'joe';
$username = ucwords(strtolower(trim($username)));
$filename = 'users.txt';
if(file_exists($filename)){
$users= array_map('trim',file($filename));
if(in_array(strtolower($username), array_map('strtolower', $users))){
    echo "Got $username";
} else {
    echo "$username not found";
}
} else {
    echo "$filename doesn't exist.";
}
?>

Archived

This topic is now archived and is closed to further replies.

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