Skip to content
Advertisement

Backslash in json_encode (PHP) + JSON_ARRAYAGG (mySQL)

The problem is the backslashes with the quotes in the array colors. I think it’s because the JSON_ARRAYAGG but I don’t know how to print a correct json.

Query:

JavaScript

PHP:

JavaScript

OUTPUT:

JavaScript

Advertisement

Answer

You are encoding colors twice. First in SQL and then in PHP. What you can do is either decode colors in PHP before you encode $data:

JavaScript

If you fetch as objects, then use this:

JavaScript

Or generate the JSON completely in SQL:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement