The effect that
needs to achieve is to convert GBK array to utf-8 array to the Json_encode function, which is described in detail in the following conversion methods
Because some special character's display effect reason had to change the custom Utf-8 project to the GBK, because uses the AJAX technology, also involves the old problem--the coding transformation. Some form validation needs to return JSON data, PHP's Json_encode function only supports UTF-8 encoding, but only iconv, the effect that needs to be achieved is to convert GBK array to utf-8 function. The first idea, the array after the serialization of the ICONV function to convert the encoding, and then deserialized, the code is as follows: code is as follows: Unserialize (Iconv (' GBK ', ' Utf-8 ', serialize ($ Array))); The resulting results are blank, and later remembered to have the default encoding Ini_set (' default_charset ', ' GBK ') set in the config file. It is certainly not good to use GBK to deserialize utf-8 strings, where a ini_set (' Default_charset ', ' utf-8 ') is added between serialization and deserialization; It should be OK, but it always feels a bit awkward, because it is the global coding setting that can easily lead to coding problems elsewhere, such as database operations. So to change the idea, using the serialization method of building array prototype, with the help of Var_export function, the final function is as follows: The code is as follows: function Array_iconv ($in _charset, $out _charset, $arr) { return eval (iconv ($in _charset, $out _charset,var_export ($arr, True). } principle is very simple var_export set the second argument to true, return the array prototype string, convert the string to Utf-8 encoding, and then perform the return with eval (similar to an anonymous function?). ), to solve the problem perfectly. Follow-up: Later on the internet to search the data, see if there is a better way to find the same, are using recursive call Iconv way, if the array element is too much or more than a few dimensions, performance certainly not how, better is the way of native code, No need to consider whether an n-dimensional array or associative array, everything has been automatically completed, to ensure that the array conversion before and after the data consistent. From the length of the code and the comparison of the cyclic and native methods, I believe you already have a choice.