Home > Backend Development > PHP Problem > How to convert JSON format with Chinese characters into an array in PHP

How to convert JSON format with Chinese characters into an array in PHP

PHPz
Release: 2023-04-20 09:34:46
Original
775 people have browsed it

PHP is a popular server-side programming language used for building web applications. JSON (JavaScript Object Notation) is a lightweight data exchange format. It is a text format that is easy for humans to understand and machines to process. PHP has built-in functions for processing JSON data and provides functions to convert JSON format into PHP arrays. However, when the JSON data contains Chinese characters, garbled characters will appear. In this article, we will introduce how to convert the JSON format with Chinese characters into a PHP array and solve the garbled problem.

  1. What is JSON format

JSON format is a lightweight data exchange format that is based on the syntax of the JavaScript language but can also be used with other programming languages. . JSON consists of key-value pairs, each key-value pair is separated by a comma, and the key and value are separated by a colon. Both arrays and objects can be used in JSON. The following is an example of JSON format:

{
    "name": "张三",
    "age": 25,
    "gender": "男",
    "hobbies": ["游泳", "唱歌", "阅读"],
    "address": {
        "province": "广东",
        "city": "深圳"
    }
}
Copy after login
  1. JSON to PHP array

We can use PHP's json_decode function to convert JSON format to PHP array. Its syntax is as follows:

mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )
Copy after login

Among them, the parameter $json is a string in JSON format, and $assoc is an optional parameter. When its value is TRUE, an associative array will be returned, otherwise an object will be returned. $depth is an optional parameter, specifying the maximum recursion depth, which defaults to 512. $options is also an optional parameter, specifying JSON decoding options. The default is 0.

The following is an example of converting JSON format into a PHP array:

$json = '{"name": "张三", "age": 25, "gender": "男"}';
$arr = json_decode($json, true);
print_r($arr);
Copy after login

The output results are as follows:

Array
(
    [name] => 张三
    [age] => 25
    [gender] => 男
)
Copy after login
Copy after login
  1. Processing JSON format with Chinese characters

However, when the JSON data contains Chinese characters, garbled characters will appear. This is because PHP encodes JSON into UTF-8 format by default, and Chinese characters occupy 3 bytes in UTF-8. Therefore, if our JSON data contains Chinese characters, it needs to be processed during the conversion process.

In PHP, we can use the iconv function to convert different character sets. The syntax of the iconv function is as follows:

string iconv ( string $in_charset , string $out_charset , string $str )
Copy after login

Among them, the $in_charset parameter is the source character set, the $out_charset parameter is the target character set, and the $str parameter is the string to be converted. The following is an example of converting JSON format into a PHP array and solving the garbled problem:

$json = '{"name": "张三", "age": 25, "gender": "男"}';
$json = iconv('UTF-8', 'GBK//IGNORE', $json); // 将JSON转换为GBK格式
$arr = json_decode($json, true);
$arr = array_map(function($value) {
    return iconv('GBK', 'UTF-8//IGNORE', $value); // 将数组转换为UTF-8格式
}, $arr);
print_r($arr);
Copy after login

The output results are as follows:

Array
(
    [name] => 张三
    [age] => 25
    [gender] => 男
)
Copy after login
Copy after login

In the above example, we first use the iconv function to convert the JSON format String converted from UTF-8 to GBK format. Then, use the json_decode function to convert the JSON format into a PHP array. Finally, we use the array_map function to convert each value in the array from GBK format to UTF-8 format.

In addition to using the iconv function, we can also use the mb_convert_encoding function to perform character encoding conversion. The mb_convert_encoding function is similar to the iconv function, except that the parameters are slightly different. The following is an example of character encoding conversion using the mb_convert_encoding function:

$json = '{"name": "张三", "age": 25, "gender": "男"}';
$json = mb_convert_encoding($json, 'GBK', 'UTF-8'); // 将JSON转换为GBK格式
$arr = json_decode($json, true);
$arr = array_map(function($value) {
    return mb_convert_encoding($value, 'UTF-8', 'GBK'); // 将数组转换为UTF-8格式
}, $arr);
print_r($arr);
Copy after login

The output is the same as the previous example.

  1. Summary

In PHP, we can use the json_decode function to convert JSON format into a PHP array. However, when the JSON data contains Chinese characters, garbled characters will appear. To solve this problem, we can use the iconv function or the mb_convert_encoding function to convert the JSON format to the specified character set and then convert it to a PHP array. In this way, we can correctly handle the JSON format with Chinese characters.

The above is the detailed content of How to convert JSON format with Chinese characters into an array in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template