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

藏色散人
Release: 2023-03-12 13:16:01
Original
2443 people have browsed it

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 ) );
//转回数组
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template