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