Because the Json_encode () method that converts an array to JSON can only convert the Utf-8 character set, the Chinese characters in the UTF-8 format will be null, and the character in the array needs to be uniformly transcoded, but the Iconv () method can only convert string type encoding , the following methods can be found on the Internet to solve the problem of transcoding of string type and array type uniformly
function Auto_charset ($fContents, $from = ' GBK ', $to = ' utf-8 ') {
$from = Strtoupper ($from) = = ' UTF8 '? ' Utf-8 ': $from;
$to = Strtoupper ($to) = = ' UTF8 '? ' Utf-8 ': $to;
if (Strtoupper ($from) = = = Strtoupper ($to) | | empty ($fContents) | | (Is_scalar ($fContents) &&!is_string ($fContents))) {
return $fContents;
}
if (is_string ($fContents)) {
if (function_exists (' mb_convert_encoding ')) {
Return mb_convert_encoding ($fContents, $to, $from);
} else if (function_exists (' Iconv ')) {
Return Iconv ($from, $to, $fContents);
} else {
return $fContents;
}
} else if (Is_array ($fContents)) {
foreach ($fContents as $key = = $val) {
$_key = Auto_charset ($key, $from, $to);
$fContents [$_key] = Auto_charset ($val, $from, $to);
if ($key! = $_key)
Unset ($fContents [$key]);
}
return $fContents;
}
else {
return $fContents;
}
}
How PHP implements Transcoding (supports array type transcoding)