In recent years, with the continuous development of Web technology, data interaction between the front end and the back end has become more and more common. In this process, data format conversion is particularly important. This article will introduce how to convert Chinese array to json format in php.
1. What is a Chinese array
Chinese arrays are similar to ordinary arrays, except that the key names and values of the array elements are Chinese strings. For example:
$cn_arr = array("姓名"=>"张三","年龄"=>20,"性别"=>"男");
2. What is JSON
JSON (JavaScript Object Notation) is a language for data exchange. It represents data in a concise form and is easy to read and write. JSON can represent numbers, strings, Boolean values, arrays, objects, etc., and is widely used in web applications. For example:
{ "name": "John", "age": 30, "city": "New York" }
3. Convert Chinese array to JSON
In PHP, we can use the json_encode() function to convert the Chinese array into JSON format. For example: The JSON_UNESCAPED_UNICODE parameter in the
$cn_arr = array("姓名"=>"张三","年龄"=>20,"性别"=>"男"); $json_str = json_encode($cn_arr,JSON_UNESCAPED_UNICODE); echo $json_str;
code is to prevent Chinese garbled characters, which means that Chinese will not be Unicode encoded.
If we want to output neat JSON format, we can use the second parameter JSON_PRETTY_PRINT of the json_encode() function. For example:
$cn_arr = array("姓名"=>"张三","年龄"=>20,"性别"=>"男"); $json_str = json_encode($cn_arr,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); echo $json_str;
The output result is as follows:
{ "姓名": "张三", "年龄": 20, "性别": "男" }
It should be noted that the json_encode() function can only handle UTF-8 encoded strings. If your PHP file encoding is not UTF-8, you need to use the mb_convert_encoding() function to convert the array elements into UTF-8 encoded strings, for example:
$cn_arr = array("姓名"=>"张三","年龄"=>20,"性别"=>"男"); $utf8_arr = array(); foreach($cn_arr as $key=>$value){ $utf8_arr[mb_convert_encoding($key,"UTF-8","auto")]=mb_convert_encoding($value,"UTF-8","auto"); } $json_str = json_encode($utf8_arr,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT); echo $json_str;
4. JSON to Chinese array
In php, we can use the json_decode() function to convert a JSON-formatted string into an array. For example:
$json_str = '{"姓名":"张三","年龄":20,"性别":"男"}'; $cn_arr = json_decode($json_str,true); print_r($cn_arr);
The second parameter true in the code means converting the JSON object into an array. The output result is as follows:
Array ( [姓名] => 张三 [年龄] => 20 [性别] => 男 )
It should be noted that the json_decode() function can only handle UTF-8 encoded strings. If your JSON string encoding is not UTF-8, you need to use the mb_convert_encoding() function to convert the string to UTF-8 encoding, for example:
$json_str = '{"姓名":"张三","年龄":20,"性别":"男"}'; $utf8_str = mb_convert_encoding($json_str,"UTF-8","auto"); $cn_arr = json_decode($utf8_str,true); print_r($cn_arr);
5. Summary
This article introduces Method to convert Chinese array to JSON format in php, and how to convert JSON string to Chinese array. When converting data formats, special attention needs to be paid to encoding issues to avoid garbled characters.
The above is the detailed content of How to convert Chinese array to json in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools