Examples of PHP array encoding conversion

Source: Internet
Author: User

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.  

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.