PHP read and write JSON from file

The clue is in the error message – if you look at the documentation for json_decode note that it can take a second param, which controls whether it returns an array or an object – it defaults to object.

So change your call to

$json = json_decode(file_get_contents($file), true);

And it’ll return an associative array and your code should work fine.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)