Home > Backend Development > PHP Tutorial > Solution to the problem of PHP json_encode Chinese garbled characters_PHP tutorial

Solution to the problem of PHP json_encode Chinese garbled characters_PHP tutorial

WBOY
Release: 2016-07-21 16:12:30
Original
846 people have browsed it

The following PHP code can solve the following problems:
1.json_encode The string after UTF8 code Chinese cannot be read
2.json_encode multi-level array Chinese garbled problem
3.json_encode error when the array contains line breaks
4.json_encode The problem with the keys in the array being Chinese

Copy the code The code is as follows:

function _encode($arr)
{
$na = array();
foreach ( $arr as $k => $value ) {
$na[_urlencode($k)] = _urlencode ($value);
}
return addcslashes(urldecode(json_encode($na)),"\r\n");
}

function _urlencode($elem)
{
if(is_array($elem)){
foreach($elem as $k=>$v){
$na[_urlencode($k)] = _urlencode($v);
}
return $na;
}
return urlencode($elem);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313650.htmlTechArticleThe following PHP code can solve the following problems: 1.json_encode The string after UTF8 code Chinese cannot be read 2.json_encode Multi-level array Chinese garbled problem 3. When the json_encode array contains line breaks...
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