Home > Backend Development > PHP Problem > How to convert Chinese array to json in php

How to convert Chinese array to json in php

PHPz
Release: 2023-04-18 10:05:44
Original
630 people have browsed it

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,"性别"=>"男");
Copy after login

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"
}
Copy after login

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;
Copy after login

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;
Copy after login

The output result is as follows:

{
    "姓名": "张三",
    "年龄": 20,
    "性别": "男"
}
Copy after login

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;
Copy after login

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);
Copy after login

The second parameter true in the code means converting the JSON object into an array. The output result is as follows:

Array
(
    [姓名] => 张三
    [年龄] => 20
    [性别] => 男
)
Copy after login

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);
Copy after login

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!

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