Home  >  Article  >  Backend Development  >  PHP json_encode中文乱码问题的解决办法_php技巧

PHP json_encode中文乱码问题的解决办法_php技巧

WBOY
WBOYOriginal
2016-05-17 08:55:321019browse

下面的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);
}
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