Home >Backend Development >PHP Tutorial >PHP array encoding conversion example demonstration_PHP tutorial
Using the serialization method of constructing an array prototype, with the help of the var_export function, the final function is as follows:
The code 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 very simple. var_export sets the second parameter to true, returns the array prototype string, converts the string to utf-8 encoding, and then uses eval to perform the return (similar to an anonymous function?), thus solving the problem perfectly.
The editor of Yiju Tutorial has another idea, which is to use foreach to traverse the array and then use the iocnv function to convert one by one, but this conversion is to convert the array value. Using the serialization method of constructing the array prototype, with the help of the var_export function, the final function is as follows: The code is as follows function array_iconv($in_charset ,$out_charset,$arr){ return eval('return '.iconv($...