Is this what you want?
echo json_encode($artists)
PHP: json_encode
Answer from Nikola Drosakis on Stack OverflowVideos
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
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.
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);
If you are not using dataType : 'json', you might need to do stripslashes
$.post(window.data.baseUrl, {posts : JSON.stringify(posts)});
And in php:
$posts = json_decode(stripslashes($_POST['posts']));
This helped me:
data = json_decode($this->request->data['jsarr'], true);
in your php code for accessing the record
Hope it will help someone!
First make sure that $locationinfo is exactly a json string. I suspect it is a php associative array.
Try echo $locationinfo['id'];. If value appears u don't want to decode it. Use
$locationinfo directly withot json decode.
If it is a json, Try using like this,
$arr = json_decode($locationinfo, true);
Add a stripslashes.
$data = json_decode(stripslashes($data),true);
Demo : http://codepad.org/XX9QD3iX
Answered here : https://stackoverflow.com/a/37599821/19168006
Edit : example in demo has stdClass error, this is the working one : http://codepad.org/lfJJu5yA