0

I am writing some values into an array from a comma separated text file like this...

$tokenFile  = "tokens.txt";
$tokenSet = file_get_contents($tokenFile);
$tokenValues = explode(',', $tokenSet);   

foreach($tokenValues as $value) {
        $tokens[] = "$value";
    };

And I get this with print_r($tokens);

Array ( [0] => token1 [1] => token2 [2] => token3 )

This is all good and exactly what I want, I think, but then if I use in_array to check for one of those values I am not getting the results I want.

if (in_array('token2', $tokens)) {
    echo "token found!";
};

If I create my $tokens array like below instead it works fine.

$tokens = array('token1', 'token2', 'token3')

And once again with print_r($tokens) I get..

Array ( [0] => token1 [1] => token2 [2] => token3 )

Both arrays appear exactly the same. What am I missing.

Update So the arrays are different

with var_dump I get the following array from the foreach loop

array(4) {
  [0]=>
  string(6) "token1"
  [1]=>
  string(8) "token2"
  [2]=>
  string(8) "token3"
  }

And I get the this from the other array.

array(4) {
  [0]=>
  string(6) "token1"
  [1]=>
  string(6) "token2"
  [2]=>
  string(6) "token3"
  }
5
  • Both Arrays are not same. One is single dimensional and other is Multidimensional. Use foreach loop. Commented Feb 23, 2015 at 11:27
  • do (with your array from the file)var_dump($tokens); Right click -> view source code and show us what you get there Commented Feb 23, 2015 at 11:29
  • See the difference: string(8) "token3" and string(6) "token3" Commented Feb 23, 2015 at 11:45
  • @Rizer123 Okay I updated my question above Commented Feb 23, 2015 at 11:45
  • possible duplicate of Why does my PHP string comparison fail? Commented Feb 23, 2015 at 11:46

1 Answer 1

1

I suppose you forgot about spaces or \n \r symbols. Try to use var_dump() instead of print_r to see the difference in strings. And don't forget to use trim() function to remove extra symbols on both ends of a string.

$tokenFile  = "tokens.txt";
$tokenSet = file_get_contents($tokenFile);
$tokenValues = explode(',', $tokenSet);   

foreach($tokenValues as $value) {
    $tokens[] = trim($value);
};
var_dump($tokens);

Another way to get values is to use regular expressions.

Sign up to request clarification or add additional context in comments.

5 Comments

Why do you know that there are still spaces? Did you saw the output of var_dump() from OP?
Thats the only way to have the same print_r output and different in_array behaviour for strings, that doesn't contain spaces in the middle.
Ah okay and what would be about html tags? You don't see them neither in print_r() nor in var_dump()
@shukshin Bingo! That was it. Works perfectly now. Thanks.
Please, colleagues, don't downvote my old dummy questions @Rizier123, that's easy: he stores tokens, not html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.