JSON stands for JavaScript Object Notation. It's a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In PHP, the built-in json_encode() function allows developers to convert arrays or objects into JSON format for data communication between front-end and back-end systems. This function has been available since PHP 5.2.0.
By default, json_encode() converts Chinese characters into Unicode escape sequences. For example:
$arr = array('name' => '张三', 'age' => 18, 'gender' => '男');
echo json_encode($arr);
// Output: {"name":"\u5f20\u4e09","age":18,"gender":"\u7537"}
As shown above, the Chinese characters are replaced with their Unicode representations, which can result in unreadable output in certain environments.
Starting from PHP 5.4.0, json_encode() supports the JSON_UNESCAPED_UNICODE option. Adding this flag as the second parameter prevents Chinese characters from being escaped:
$arr = array('name' => '张三', 'age' => 18, 'gender' => '男');
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
// Output: {"name":"张三","age":18,"gender":"男"}
This method is straightforward and highly recommended, as long as your PHP version supports it.
If you're using a PHP version older than 5.4.0 or cannot use JSON_UNESCAPED_UNICODE, you can handle Chinese characters manually using urlencode() and urldecode() functions:
$arr = array('name' => '张三', 'age' => 18, 'gender' => '男');
$new_arr = array();
foreach($arr as $key => $value) {
$new_arr[$key] = urlencode($value);
}
echo urldecode(json_encode($new_arr));
// Output: {"name":"张三","age":18,"gender":"男"}
This method ensures proper output without Unicode encoding, even in older PHP environments.
Both solutions effectively prevent Chinese characters from being encoded as Unicode in JSON. Developers can choose the appropriate method based on their PHP version and project requirements. Proper handling of character encoding ensures better readability and enhances user experience during front-end and back-end data exchange.