PHP's json_encode and JS's JSON.stringify

In JavaScript, a JSON key without quote is valid. In PHP, a JSON key without quote is NOT valid. (In fact, the right JSON syntax is with quotes on keys.)

So you’re right, the difference came from JSON.stringify who strip the quotes from your integer key.


I was having the same issue where I wanted to compare an encrypted version of the encoded json string. To make the output of json_encode identical to javascripts JSON.stringify you can do this:

$php_string = json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);