php將數組轉為json出現中文亂碼的解決方法:首先將數組中的每個元素進行urlenconde()轉碼;然后再使用json_encode()函數把每個元素轉為json即可。
解決思路:
為數組里的每個元素進行urlencode()轉碼,然后再用json_encode()轉為json即可。解碼使用json_decode()。
舉例:
foreach ( $result as $keys => $value ) //包含中文的二維數組$result轉json,數組內部元素一一使用urlencode轉換即可保證中文不亂碼 { foreach($value as $key=>$column){ $testJSON[$keys][$key] = urlencode ( $column ); } } var_dump( json_encode ( $testJSON ));//轉為json $result = urldecode ( json_encode ( $testJSON ) );//轉回數組