Home > Backend Development > PHP Tutorial > PHP json_encode中文乱码问题的解决办法_PHP

PHP json_encode中文乱码问题的解决办法_PHP

WBOY
Release: 2016-06-01 12:03:49
Original
769 people have browsed it

JSON

下面的PHP代码可以解决以下问题:
1.json_encode UTF8码中文后的字符串不可阅读
2.json_encode 多级数组中文乱码问题
3.json_encode 数组中包含换行时错误问题
4.json_encode 数组中键为中文的问题
复制代码 代码如下:
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);
}
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