Php array encoding conversion method first
$ Data = eval ('Return '. iconv ('gbk', 'utf-8', var_export ($ data, true )).';');
The var_export function can output or return a string representation of a variable. This function returns the structure information about the variables passed to this function. it is similar to var_dump ()
Similarly, the difference is that the returned representation is a valid php code.
You can set the second parameter of the function to TRUE to return the expression of the variable.
Next we will use this feature to convert the array into a string and then iconv and then use the eval function to restore it to an array. The result is displayed correctly.
Method 2
<? Phpclass arrayiconv {static protected $ in; static protected $ out;/*** static method, this method inputs an array and returns the array ** @ param unknown_type $ array input array * @ param unknown_type $ in input array encoding * @ param unknown_type $ out returns the array encoding * @ return array returned by unknown */static public function Conversion ($ array, $ in, $ out) {self ::$ in = $ in; self ::$ out = $ out; return self: arraymyicov ($ array );} /*** internal method, loop array ** @ param unknown_type $ array * @ return unknown */static priva Te function arraymyicov ($ array) {foreach ($ array as $ key => $ value) {$ key = self: myiconv ($ key); if (! Is_array ($ value) {$ value = self: myiconv ($ value);} else {$ value = self: arraymyicov ($ value );} $ temparray [$ key] = $ value;} return $ temparray ;} /*** replace array encoding ** @ param unknown_type $ str * @ return unknown */static private function myiconv ($ str) {return iconv (self ::$ in, self:: $ out, $ str) ;}}$ B = array ("test" => array ("test" => "test", "test" => "test ", "Test" => array ("test" => "test"), "fasdf" => "test", "test" => "test"); $ a = arrayiconv: Conversion ($ B, "UTF-8 "," Gb2312 "); print_r ($ a);?>