Home >Backend Development >PHP Problem >How to solve the problem of double quotes when converting php array to json

How to solve the problem of double quotes when converting php array to json

PHPz
PHPzOriginal
2023-04-18 15:22:271712browse

In the process of converting PHP arrays to JSON data, sometimes you will encounter situations where the JSON string contains double quotes. This situation is very common in actual development, and the solution is very simple.

Generally, to convert a PHP array into a JSON string, you can use the PHP built-in function json_encode() to complete. This function converts a PHP array into a JSON string. For example, the following code converts a PHP array containing some data into a JSON string:

$data = array(
    'name' => 'John',
    'age' => 30,
    'gender' => 'male'
);

$json_str = json_encode($data);
echo $json_str;

The output of this code is:

{"name":"John","age":30,"gender":"male"}

In the above code, a string containing Convert the PHP array of three field values ​​name, age and gender into a JSON string.

However, sometimes the value in our PHP array itself contains double quotes, and problems will occur when using the json_encode() function to convert it into a JSON string.

For example, the following PHP array contains values ​​with double quotes:

$data = array(
    'name' => 'John "The Rock" Johnson',
    'age' => 30,
    'gender' => 'male'
);

If you use the json_encode() function directly to convert to a JSON string:

$json_str = json_encode($data);
echo $json_str;

The output result is:

{"name":"John "The Rock" Johnson","age":30,"gender":"male"}

We will find that the value of the name field in the JSON string is not correctly converted into a string within double quotes, but Truncated at the first double quote.

In order to solve this problem, we need to add a parameter JSON_UNESCAPED_SLASHES when using the json_encode() function to convert it into a JSON string, for example:

$json_str = json_encode($data, JSON_UNESCAPED_SLASHES);
echo $json_str;

In this way, the output result will correctly convert the value of the name field into a string within double quotes:

{"name":"John \"The Rock\" Johnson","age":30,"gender":"male"}

Note that the name field in the output result The value has been correctly enclosed in double quotes, and the internal double quotes have been escaped to \". In this way, the converted JSON string can be parsed correctly.

In addition to the JSON_UNESCAPED_SLASHES parameters, there are some other parameters that can be used to control the conversion results of the json_encode() function. If you need to know more, you can refer to the PHP official documentation json_encode() Function description in .

The above is the detailed content of How to solve the problem of double quotes when converting php array to json. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn