I have a .json file with this content:
{ "questions": "reponse" }
And I would like parse the content of the file into a PHP array but I have a strange issue…
$path = 'myFile.json';
echo file_get_contents($path);
echo var_dump(json_decode(file_get_contents($path), true));
echo var_dump(json_decode(utf8_encode(file_get_contents($path), true)));
$json = '{ "questions": "reponse" }';
echo var_dump(json_decode($json, true));
And the result on my screen is:
{ "questions": "reponse" }
null
null
array (size=1)
'questions' => string 'reponse' (length=7)
What’s the difference between the string from the file and the string in my program?
Thank you!
Try run this code:
I expect that there are some whitespaces (like a BOM/UTF-8 header) which make trouble.