How to convert Chinese array to json in php
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

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.

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 implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

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 symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159
