Solution to the problem where the json_encode function is encoded as null in Chinese-PHP source code

Source: Internet
Author: User
Json_encode is generally normal for English and uft8 encoding, but if it is gkb or Chinese, it will encounter some problems, today, let's take a look at a solution to the problem that json_encode functions are encoded as null in Chinese, however, if it is gkb or Chinese, we will encounter some problems. Today we will take a look at a solution to the problem that the Chinese character of the json_encode function is encoded as null.

Script ec (2); script

Json format is widely used in development. In php, The json_encode function can directly convert arrays into json format, which is very convenient. However, when using the json_encode function, you may find that Chinese characters are encoded as null. Originally, json only supports escaping UTF-8 encoded Chinese characters. The reason why php arrays are encoded as null using the json_encode function Chinese is that when gbk is escaped or another encoding is used, Chinese characters are ignored. Generally appear in the document encoding or output content encoding is not UTF-8, that is to say, GBK or GB2312 Chinese, there will be encoding Failure phenomenon.
The reason why php arrays are encoded as null using the json_encode function. If your program uses UTF-8 encoding, ensure that the file is saved as UTF-8 without bom format, if your program is gbk, convert it to UTF-8 encoding before using the json_encode function.

The json_encode () built-in function (php> 5.2) in php can be used to transmit and use data in php with other languages. This function converts a value to a json data storage format, but the converted Chinese character is converted to Unicode encoding.

$ Arr = array
(
'Name' => 'chia ',
'Age' => 20
);

$ Jsonencode = json_encode ($ arr );
Echo $ jsonencode;
?>
The program running result is as follows:

{"Name": null, "Age": 20}
In the json_encode function, Chinese characters are encoded as null. After Google, it is very simple. To work closely with the front-end, Json only supports UTF-8 encoding. I think the front-end Javascript is also the reason for UTF-8 encoding.

$ Array = array
(
'Title' => iconv ('gb2312', 'utf-8', 'Here is the Chinese title '),
'Body' => 'abcd... '
);

Echo json_encode ($ array );
?>
The running result of this program is:

{"Title": "\ u8fd9 \ u91cc \ u662f \ u4e2d \ u6587 \ u6807 \ u9898", "body": "abcd ..."}
All Chinese characters in the group are missing after json_encode or \ u2353 is displayed. The solution is to use the urlencode () function to process the following. Before json_encode, urlencode () is used to process all the content in the array, and json_encode () is used to convert the content into a json string, finally, use urldecode () to convert the Encoded chinese characters back.

/*************************************** ***********************
*
* Use a specific function to process all elements in the array
* @ Param string & $ array the string to be processed
* @ Param string $ function the function to be executed
* @ Return boolean $ apply_to_keys_also whether it 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 characters)
* @ Param array $ array the array to be converted
* @ Return string the converted json string
* @ Access public
*
**************************************** *********************/
Function JSON ($ array ){
ArrayRecursive ($ array, 'urlencode', true );
$ Json = json_encode ($ array );
Return urldecode ($ json );
}

$ Array = array
(
'Name' => 'chia ',
'Age' => 20
);

Echo JSON ($ array );
?>
The operation is successful. The result is as follows:

{"Name": "SIA", "Age": "20 ″}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.