Php json_encodeUTF-8 Chinese garbled better solution

Source: Internet
Author: User
Online find said json_encode set to UTF-8 Chinese will not garbled, after a period of time is not useful, the following to share with you a better method recently used in the interface code json_encode, on the Internet find said json_encode encoding set to UTF-8 Chinese won't garbled, verified this method is indeed effective, but do not know why, the code won't work after a while. The following is your solution to json_encode. There are better ways to share it!

First:

This simple code conversion, urlcode and then return the required array

My code is enough.

The code is as follows:

public static function encodeOperations ($array){foreach ((array)$array as $key => $value) {if (is_array($value)) {encodeOperations($array[$key]);} else {$array[$key] = urlencode(mb_convert_encoding($value,'UTF-8','GBK'));}}return $array;}

Second:

This is seen on the Internet, and then there is a comment saying that there will be an infinite loop problem, but this is obviously very comprehensive, and I did have it after testing, posted here for reference only

The code is as follows:

/*************************************** * *********************** Use a specific function to process all elements in the array * @ param string & $ array: the string to be processed * @ param string $ After tocode encoding * @ param string $ Before oldcode encoding * @ param string $ function: the function to be executed * @ return boolean $ apply_to_keys_also? apply * @ return array $ array to key * @ access public ******************** **************************************** **/function encodeOperations (& $ array, $ Function, $ tocode = false, $ oldcode = false, $ apply_to_keys_also = false) {foreach ($ array as $ key => $ value) {(www.bitsCN.com) if (is_array ($ value) {encodeOperations ($ array [$ key], $ function, $ apply_to_keys_also);} else {if ($ tocode & $ oldcode) {if (function_exists (mb_convert_encoding) {$ value = mb_convert_encoding ($ value, $ tocode, $ oldcode);} else {return "error ";}} $ array [$ key] = $ function ($ value);} if ($ ap Ply_to_keys_also & is_string ($ key) {$ new_key = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}} return $ array ;} /***** from: http://www.bitsCN.com/phper/31/66729.htm **************************************** * ******************* 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 );}

If the return value for Chinese characters is null, convert the characters to uft8 or use urlencode to compile the call.

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.