The JSON format is widely used in development. In PHP, the Json_encode function can directly convert an array into JSON format, which is very convenient. But it is possible that when you use the Json_encode function, you will find that Chinese is encoded as null. Originally, JSON only supports escaping UTF-8 encoded format in Chinese. The reason the PHP array uses the Json_encode function in Chinese is encoded as null because it escapes the GBK or other encoding, the Chinese is ignored. Generally appear in the document encoding or output of the content encoding is UTF-8, that is, GBK or GB2312 in Chinese, there will be the phenomenon of coding failure.
PHP array uses the Json_encode function Chinese is encoded into null reason and the solution, if your program is to use UTF-8 code, please ensure that the file is saved as Utf-8 no BOM format, if your program is GBK, can be converted to UTF-8 encoded after the use of Json_ Encode function
Use the Json_encode () built-in function (Php > 5.2) in PHP to use the data in PHP to communicate well with other languages and use it. The function is to convert the numeric value into the JSON data store format, but the converted Chinese will become Unicode encoded.
<?php
$arr = array
(
' Name ' => ' Shia ',
' Age ' =>20
);
$jsonencode = Json_encode ($arr);
Echo $jsonencode;
?>
The results of the program operation are as follows:
{' Name ': null, ' Age ': 20}
Json_encode function in Chinese is encoded into null, Google a bit, very simple, in order to close with the front-end, Json only support utf-8 encoding, I think the front-end Javascript is also the reason for Utf-8.
<?php
$array = array
(
' title ' =>iconv (' gb2312′, ' utf-8′, ' here is the Chinese title '),
' Body ' => ' abcd ... '
);
echo Json_encode ($array);
?>
The results of this program are:
{"title": "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898″," "Body": "ABCD ..."}
All Chinese in the array are missing or appear \u2353 after Json_encode. The solution is to use the UrlEncode () function to process the following, Json_encode all the contents of the array in UrlEncode (), and then convert the Json_encode () to the JSON string, and then the UrlDecode () Turn the encoded Chinese back in.
<?php
/**************************************************************
*
* Handle all elements in the array with a specific function
* @param string & $array strings to be processed
* @param string $function the function to execute
* @return Boolean $apply _to_keys_also is also applied to the key
* @access Public
*
*************************************************************/
Function arrayrecursive (& $array, $function, $apply _to_keys_also = False)
{
static $recursive _counter = 0;
if (+ + $recursive _counter > 1000) {
Die (' Possible deep recursion attack ');
}
foreach ($array as $key => $value) {
if (Is_array ($value)) {
Arrayrecursive ($array [$key], $function, $apply _to_keys_also);
} else {
$array [$key] = $function ($value);
}
if ($apply _to_keys_also && is_string ($key)) {
$new _key = $function ($key);
if ($new _key!= $key) {
$array [$new _key] = $array [$key];
Unset ($array [$key]);
}
}
}
$recursive _counter–;
}
/**************************************************************
*
* Convert an array to a JSON string (compatible with Chinese)
* @param array $array arrays to convert
* @return string converted to a JSON string
* @access Public
*
*************************************************************/
function JSON ($array) {
Arrayrecursive ($array, ' UrlEncode ', true);
$json = Json_encode ($array);
Return UrlDecode ($json);
}
$array = array
(
' Name ' => ' Shia ',
' Age ' =>20
);
echo JSON ($array);
?>
This success, the results of the operation are as follows:
{' Name ': ' Shia ', ' age ': ' 20″}