0

I get a strange behavior when working with an array in my application, that is array_key_exists returns false when it should return true. I write this simple code :

function keyExists($data){
    var_export($data);  echo "<BR>";
    echo "array_key_exists('key0',data) : ";
    if(array_key_exists('key0', $data)) echo "TRUE"; else echo "FALSE"; echo "<BR>";
    echo "array_key_exists('key1',data) : ";
    if(array_key_exists('key1', $data)) echo "TRUE"; else echo "FALSE"; echo "<BR>";
    echo "array_key_exists('key2',data) : ";
    if(array_key_exists('key2', $data)) echo "TRUE"; else echo "FALSE"; echo "<BR>";
    echo "array_key_exists('key3',data) : ";
    if(array_key_exists('key3', $data)) echo "TRUE"; else echo "FALSE"; echo "<BR>";
    echo "array_key_exists('key4',data) : ";
    if(array_key_exists('key4', $data)) echo "TRUE"; else echo "FALSE"; echo "<BR>";
    echo "<BR><BR>";
}
keyExists(["key1" => "value 1",
           "key2" => "value 2",
           "key3" => "value 3",
           "key4" => "value 4"]);

keyExists(["key2" => "value 2",
           "key1" => "value 1",
           "key3" => "value 3",
           "key4" => "value 4"]);

keyExists(["key0" => "value 0", 
           "key2" => "value 2",
           "key1" => "value 1",
           "key3" => "value 3",
           "key4" => "value 4"]);

keyExists(["key0" => "value 0",
           "key1" => "value 1",
           "key2" => "value 2",
           "key3" => "value 3",
           "key4" => "value 4"]);

keyExists(array("key0" => "value 0",
                "key1" => "value 1",
                "key2" => "value 2",
                "key3" => "value 3",
                "key4" => "value 4"));

I run it on windows wamp with Php 7.4 and on Debian production server with Php 7.4, I get the same result :

array ( 'key1' => 'value 1', 'key2' => 'value 2', 'key3' => 'value 3', 'key4' => 'value 4', )
array_key_exists('key0',data) : FALSE
array_key_exists('key1',data) : FALSE // ???
array_key_exists('key2',data) : TRUE
array_key_exists('key3',data) : TRUE
array_key_exists('key4',data) : TRUE


array ( 'key2' => 'value 2', 'key1' => 'value 1', 'key3' => 'value 3', 'key4' => 'value 4', )
array_key_exists('key0',data) : FALSE
array_key_exists('key1',data) : TRUE
array_key_exists('key2',data) : FALSE // ???
array_key_exists('key3',data) : TRUE
array_key_exists('key4',data) : TRUE


array ( 'key0' => 'value 0', 'key2' => 'value 2', 'key1' => 'value 1', 'key3' => 'value 3', 'key4' => 'value 4', )
array_key_exists('key0',data) : TRUE
array_key_exists('key1',data) : TRUE
array_key_exists('key2',data) : FALSE // ???
array_key_exists('key3',data) : TRUE
array_key_exists('key4',data) : TRUE


array ( 'key0' => 'value 0', 'key1' => 'value 1', 'key2' => 'value 2', 'key3' => 'value 3', 'key4' => 'value 4', )
array_key_exists('key0',data) : TRUE
array_key_exists('key1',data) : TRUE
array_key_exists('key2',data) : FALSE // ???
array_key_exists('key3',data) : TRUE
array_key_exists('key4',data) : TRUE


array ( 'key0' => 'value 0', 'key1' => 'value 1', 'key2' => 'value 2', 'key3' => 'value 3', 'key4' => 'value 4', )
array_key_exists('key0',data) : TRUE
array_key_exists('key1',data) : TRUE
array_key_exists('key2',data) : FALSE // ???
array_key_exists('key3',data) : TRUE
array_key_exists('key4',data) : TRUE

I also tested same code with isset instead of array_key_exists, same result.

user2244705
  • 361
  • 4
  • 16
  • 3
    You have zero-width non-breaking spaces in these keys (\uFEFF). – lukas.j Feb 28 '22 at 11:38
  • He's [right](https://imgur.com/23iLyx4) – Cid Feb 28 '22 at 11:42
  • @Lukas can you elaborate ? – user2244705 Feb 28 '22 at 13:09
  • What you have are keys which look like this: ' key2', but the space is a zero-width space, so it is shown as 'key2' in your IDE and on this website. But there is that special kind of space as first character in these keys, which do return false on _array_key_exists()_. – lukas.j Feb 28 '22 at 13:14
  • Ok Lukas, so I typed a file from scracth using notepad++, format UTF8. The first line is the header (database field names), the next is data (field values). The header is read, then put in an array using explode. I still have the issue with the first element. How is it possible to get a non printable character typing a file manually ? (note the file format is UTF8 without BOM) – user2244705 Feb 28 '22 at 13:38
  • Anyway, even notepad++ can't show the zero-width space. Is there any tool able to do that ? – user2244705 Feb 28 '22 at 13:46
  • Thanks Lukas. To conclude, your comment is right about zero-width space cause I retyped the full sample code from scratch and I get the right result now. I can reproduce the problem from any data file I type but I guess something other is wrong while I import the file, maybe a BOM inserted but it is antoher issue in code. – user2244705 Mar 01 '22 at 13:09

0 Answers0