解决 json_encode 中文乱码

来源: http://www.phpchina.com/archives/view-42572-1.html

解决以下问题

    1. json_encode 中文后的字符串不可阅读
    2. json_encode 多级数组中文乱码问题
    3. json_encode 数组中包含换行时错误问题
    4. json_encode 数组中键为中文的问题
    5. http://blog.1dnet.net/?post=840

【php代码】

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);
}

 

posted on 2013-01-09 16:55  mingaixin  阅读(413)  评论(0编辑  收藏  举报