Home > Backend Development > PHP Problem > Convert php array to json to filter Chinese

Convert php array to json to filter Chinese

WBOY
Release: 2023-05-19 09:00:37
Original
597 people have browsed it

In web development, it is often necessary to convert arrays in PHP to JSON format in order to dynamically load data on the page. However, during the process of converting the array to JSON, Chinese encoding problems may occur, preventing the data from being displayed properly. This article will discuss how to filter Chinese encoding issues in the process of converting PHP arrays to JSON to ensure that the data is displayed normally on the page.

1. Convert PHP array to JSON

In PHP, use the json_encode() function to convert the array into JSON format. For example, if you have the following array:

$arr = array(
  "name" => "张三",
  "age" => 25,
  "gender" => "男"
);
Copy after login

You can convert it to JSON format through the following code:

$json = json_encode($arr);
echo $json; // 输出:{"name":"张三","age":25,"gender":"男"}
Copy after login

2. Chinese encoding problem

However, when the array contains Chinese When using characters, the converted JSON format may have encoding problems. For example, convert the following array to JSON format:

$arr = array(
  "name" => "李四",
  "age" => 30,
  "gender" => "男",
  "desc" => "这是一个描述,包含中文字符"
);
Copy after login

Use the json_encode() function to convert it to JSON format:

$json = json_encode($arr);
echo $json; // 输出:{"name":"u674eu56db","age":30,"gender":"u7537","desc":"u8fd9u662fu4e00u4e2au63cfu8ff0uff0cu5305u542bu4e2du6587u5b57u7b26"}
Copy after login

As you can see, Chinese characters are encoded as Unicode characters and cannot be used in displayed normally on the page. This is because by default, the json_encode() function encodes non-ASCII characters into Unicode characters for transferring data between different platforms.

3. Filter Chinese

In order to solve the Chinese encoding problem, you can use the JSON_UNESCAPED_UNICODE parameter to filter Chinese characters. This parameter allows the json_encode() function to not encode Chinese characters into Unicode characters when converting JSON, but to directly output Chinese characters.

Modify the above code as follows:

$json = json_encode($arr, JSON_UNESCAPED_UNICODE);
echo $json; // 输出:{"name":"李四","age":30,"gender":"男","desc":"这是一个描述,包含中文字符"}
Copy after login

You can see that Chinese characters have been output like other characters and can be displayed normally on the page.

4. Summary

When converting a PHP array to JSON format, if the array contains Chinese characters, you need to pay attention to Chinese encoding issues. You can use the JSON_UNESCAPED_UNICODE parameter to filter Chinese characters to ensure that the data is displayed normally on the page.

The above is the detailed content of Convert php array to json to filter Chinese. 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