PHP array Encoding Conversion method reference

Source: Internet
Author: User
This article describes some of the methods of encoding and converting arrays in PHP, with the need for a friend's reference.

In PHP programming, the operation of an array is sometimes associated with the problem of encoding conversion.

Because of some special characters of the display effect of the reason, so the utf-8 changed to GBK, because of the use of Ajax technology, but also involves the old problem-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.

This is initially done by serializing the array and then converting it with the Iconv function, before deserializing it:

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

The result is blank, and later remembered that the default encoding Ini_set (' default_charset ', ' GBK ') was set in the config file, so that the string used to deserialize Utf-8 with GBK is definitely not good, and there is a ini_set between serialization and deserialization (' Default_charset ', ' utf-8 '); It should be possible, too. However, because of the global encoding settings, it is easy to lead to coding problems elsewhere, such as database operations.

Using the Var_export function, the final function of the serialization method of constructing an array prototype is as follows:

 
        

Principle Analysis: 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.

Summary: Online methods, mostly using recursive call Iconv, if the array elements too much or more dimension, performance is not. Therefore, the best way to personally feel or to use the native code, do not need to consider whether it is an n-dimensional array or associative array, everything is done automatically, to ensure that the data before and after the array conversion.

The problem of encoding conversion in PHP arrays is introduced here, and we hope to help you.

  • 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.