PHP Array Encoding transformation

Source: Internet
Author: User
PHP Array Encoding Conversion
Because some special character's display effect reason has to change the custom Utf-8 project to the GBK, because uses the AJAX technology, but also involves the old question--the code conversion.

Some form validation needs to return JSON data, PHP Json_encode function only support utf-8 encoding, but only iconv, need to achieve the effect is GBK array into utf-8 array to Json_encode function.

The first idea is to serialize the array after serializing it with the Iconv function, and then deserialize the code as follows:

Unserialize (Iconv (' GBK ', ' Utf-8 ', serialize ($array)));  

The resulting result is blank, and later remembered to configure the file to set the default encoding Ini_set (' default_charset ', ' GBK '); It is certainly not good to deserialize the Utf-8 string with GBK, where a ini_set (' Default_charset ', ' utf-8 ') is added between serialization and deserialization; It should be possible, but it's always a bit awkward, because it's a global encoding setting that can easily lead to coding problems elsewhere, such as database operations. So in a different way, using the serialization method of building an array prototype, with the help of the Var_export function, the final function is as follows:

function Array_iconv ($in _charset, $out _charset, $arr) {          return eval (' Return '. Iconv ($in _charset, $out _charset,var _export ($arr, true). '; ');  }  

The principle is simple var_export set the second argument to true, return the array prototype string, convert the string to Utf-8 encoding, and then use Eval to perform the return (like anonymous function?). ) to solve the problem perfectly.

Follow-up: Later on the internet searched the next information, see there is no better way to find all the same, are using recursive call Iconv way, if the array element too much or more dimension, performance certainly not how, better is the way of native code, do not need to consider whether it is n-dimensional array or associative array, Everything is done automatically, ensuring that the data is consistent before and after the array conversion. From the length of the code and the comparison of the cycle and the original method, I believe that we 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.