I. Using Var_export (), eval () method
/**
* Converts the Chinese array containing GBK to Utf-8 * *
@param array $arr Arrays
* @param string $in _charset The original string encoding
* @param String $out _charset Output strings encoding
* @return Array *
/function Array_iconv ($arr, $in _charset= "GBK", $out _ charset= "Utf-8")
{
$ret = eval (' Return '. Iconv ($in _charset, $out _charset,var_export ($arr, true).
return $ret;
After transcoding, you can output JSON
//Return Json_encode ($ret);
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 eval
perform the return (similar to an anonymous function?). ), to solve the problem perfectly.
Eval () Function Summary:
Condition: eval()
The function calculates the string according to the PHP code. The string must be a valid PHP code and must end with a semicolon.
Returns if the statement is not invoked in the code string return
NULL
. If there is a parse error in the code, the eval()
function returns FALSE.
$a = "Hello";
$res = eval ("return $a;");
Print_r ($res);
The $ designator must be escaped with a backslash
("\ $str = \" $str \ ";) when assigning a value;
Echo $str;
Second, using recursion to transfer the array
/**
* UTF-8 encoded GBK encoding Mutual conversion/(support array)
* *
@param array $str string, support array delivery
* @param string $in _charset The original string encoding
* @param string $out _charset Output strings encoding
* @return array
/function Array_iconv ($str, $in _ Charset= "GBK", $out _charset= "Utf-8")
{
if (Is_array ($STR))
{
foreach ($str as $k => $v)
{
$str [$k] = Array_iconv ($v);
}
return $str;
}
else
{
if (is_string ($STR))
{
//return Iconv (' UTF-8 ', ' Gbk//ignore ', $str);
Return mb_convert_encoding ($str, $out _charset, $in _charset);
else
{return
$str;
}
}
}
Summarize
PHP will be the array encoding GBK and utf-8 of the contents of the conversion to the basic end, the article introduced in detail, with a certain reference value, I hope this article for everyone's study and work can bring certain help.