Home  >  Article  >  Backend Development  >  How to solve the problem of converting php array to json Chinese garbled code

How to solve the problem of converting php array to json Chinese garbled code

藏色散人
藏色散人Original
2021-09-12 09:39:482442browse

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.

How to solve the problem of converting php array to json Chinese garbled code

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn