First off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode it later it will be just the same as it was originally. See here: https://3v4l.org/Smj2F
If you don't like the escaping though, you can use the JSON_UNESCAPED_UNICODE flag:
https://www.php.net/function.json-encode
This flag will "encode multibyte Unicode characters literally" according to https://www.php.net/manual/en/json.constants.php.
So you can do:
json_encode($array, JSON_UNESCAPED_UNICODE);
And it will give you the following output:
["M-am întîlnit ieri cu","fosta mea profă de matematică"]
Working example: https://3v4l.org/daETG
Answer from jszobody on Stack OverflowFirst off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode it later it will be just the same as it was originally. See here: https://3v4l.org/Smj2F
If you don't like the escaping though, you can use the JSON_UNESCAPED_UNICODE flag:
https://www.php.net/function.json-encode
This flag will "encode multibyte Unicode characters literally" according to https://www.php.net/manual/en/json.constants.php.
So you can do:
json_encode($array, JSON_UNESCAPED_UNICODE);
And it will give you the following output:
["M-am întîlnit ieri cu","fosta mea profă de matematică"]
Working example: https://3v4l.org/daETG
For those looking for exact equivalent result PHP json_encode() v.s. JavaScript JSON.stringify() as asked in question you must use at least these 2 flags
json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
because of for example JS produces "https://lh3.googleusercontent.com" but PHP without JSON_UNESCAPED_SLASHES produces "https:\/\/lh3.googleusercontent.com". Is important in my case because of Im creating sha hash of both and compare it. If frontend hash is the same as backend hash it means data are synced and no traffic/syncing is needed. Im not sure if some other flag is needed.
FR: change JSON::toString output to match php & javascript json_encode - Feature Requests - JUCE
php - how to use JSON.stringify and json_decode() properly - Stack Overflow
Error using json.stringify to send array to php
[PHP / JavaScript] How can I avoid removing square brackets from arrays in PHP when using json_encode
Videos
When you save some data using JSON.stringify() and then need to read that in php. The following code worked for me.
json_decode( html_entity_decode( stripslashes ($jsonString ) ) );
Thanks to @Thisguyhastwothumbs
When you use JSON stringify then use html_entity_decode first before json_decode.
$tempData = html_entity_decode($tempData);
$cleanData = json_decode($tempData);