Solution to convert php array to json Chinese garbled code: 1. Perform urlencode() transcoding for each element in the array; 2. Then use the json_encode() function to convert to json.
The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer
How to solve php array transfer json Chinese garbled problem?
Perform urlencode() for each element in the array and then use json_encode() to convert it to json. To decode, use json_decode()
Example:
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 ) ); //转回数组
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of converting php array to json Chinese garbled code. For more information, please follow other related articles on the PHP Chinese website!