7

PHP is always returning false when I use in_array(), and whether it is or isn't in the array doesn't matter. For example:

$list = 'test
list
example';
$list_arr = array();
$list_arr = explode("\n",$list);
if (in_array('test',$list_arr)) {
    echo 'Found';
}
else {
    echo 'Not Found';
}

Returns 'Not Found' even though 'test' is a value in the array

$list = 'test
list
example';
$list_arr = array();
$list_arr = explode("\n",$list);
if (in_array('qwerty',$list_arr)) {
    echo 'Found';
}
else {
    echo 'Not Found';
}

Returns 'Not Found', as it should

Why is it returning false when it should be true? Please give any suggestions. The list I am actually using has more than 10K of values and therefore I just shortened it here.

hakre
  • 193,403
  • 52
  • 435
  • 836
Ethan H
  • 717
  • 1
  • 13
  • 27

4 Answers4

11

If you are using this exact example and are coding on a Windows PC, the problem comes from the fact that Windows stores line feeds using:

\r\n

Thus, when you split on \n only you keep an invisible \r at the end of your items and they don't match for that reason.

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71
  • I am on linux, however that did fix the problem – Ethan H Jul 16 '12 at 20:29
  • It can be configured in the editor, which should not be notepad – Esailija Jul 16 '12 at 20:29
  • I also use Windows to code it, it is just running on a linux server. Could that be why? – Ethan H Jul 16 '12 at 20:31
  • 1
    I am confused, didn't you just say you are using linux? If you use notepad++, configure it to save newlines as `'\n'` (UNIX). See http://stackoverflow.com/questions/8195839/choose-newline-character-in-notepad – Esailija Jul 16 '12 at 20:31
  • As Esailija said, the source of your file has to be coded in a Linux format if you want to use PHP_EOL on linux. But if your server runs on Windows, then make sure your NotePad++ is configured for Windows style line endings. – Mathieu Dumoulin Jul 16 '12 at 20:33
  • @Esailija my server is linux but I am coding from a PC – Ethan H Jul 16 '12 at 20:34
  • We all code from PCs, well maybe some of us use XBoxes or hacked away PS3s, we practically all use a PC should it be Windows, Linux or MacOS – Mathieu Dumoulin Jul 16 '12 at 20:35
  • @MathieuDumoulin my original comment was aimed at you. As in, it depends on the editor not on the OS. Any editor worth the storage on hard drive can be configured to use `\n` for line feeds. – Esailija Jul 16 '12 at 20:41
  • I meant I am coding from Windows, my bad – Ethan H Jul 16 '12 at 20:42
8

Your list is actually separated by \r\n, not just \n.

$list_arr = explode("\r\n",$list);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

As already stated, Windows uses \r\n as end of line characters. Unix uses \n and Mac uses \r. To make portable code, use the PHP_EOL constant to explode your string:

$list_arr = explode(PHP_EOL, $list)

That way, you are sure it always works correctly.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Not true, as Windows will store EOL as \r\n but if you run it on a linux pc, it will be \n only: http://ca3.php.net/manual/en/reserved.constants.php – Mathieu Dumoulin Jul 16 '12 at 20:32
  • @MathieuDumoulin the provided link does not state that. It just says "the right end-of-line character" for the platform. My solution wouldn't work if you'd use a Windows terminated editor on a Unix platform. – Bart Friederichs Jul 16 '12 at 20:36
1

That is a pretty odd way of getting values into a list. So I am assuming that you are ultimately trying to lines from a separate file into an array. In which case you would just do

file('path/to/file');

To read each line into a separate array item.

Where the file referenced looks like:

test
list
example

If you know the values up front like in your code sample, you should just put them into an array rather than reading them from a string.

If you simply must read the values from a string, I would suggest using PHP_EOL constant to define your line breaks. Like this:

$list = 'test' . PHP_EOL . 'list' . PHP_EOL . 'example';
$list_arr = explode(PHP_EOL, $list);

The PHP_EOL constant is going to give you a much better platform-independent way of getting values defining your line breaks.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103