PHP将数组转为json出现的中文乱码的问题

3809

1.将GBK编码的含有中文数组转为json格式,先将每个值进行urlencode,然后json_encode,最后urldecode即可。

// 示例代码
$testJSON=array('title' => '标题', 'url' => 'meitu.jpg');
    foreach ( $testJSON as $key => $value ) 
    {   
        $testJSON[$key] = urlencode ( $value );   
    }   
  $ret = urldecode ( json_encode ( $testJSON ) );  
   print_r($ret );

2将json转为数组

// 将GBK转为UTF-8后,再json_decode为数组json_decode($new_url, true),后边带true参数表示转为数组,否则默认为对象形式的
  $new_url = iconv("GBK","UTF-8//IGNORE",  $ret );
  print_r(json_decode($new_url, true));