Home  >  Article  >  Backend Development  >  PHP Json Chinese processing solution

PHP Json Chinese processing solution

高洛峰
高洛峰Original
2016-12-30 13:45:591080browse

This article describes PHP's Json Chinese processing solution. Share it with everyone for your reference, the details are as follows:

Json is a widely used format for transmitting strings. Compared with xml, it is simpler to understand and easier to operate. There are only two functions in PHP. , json_encode() AND json_deconde(). However, json's support for Chinese is not very good. If you use json_encode() to process an array, if there are Chinese characters in the array, it will be blanked.

One way to solve Chinese is to first convert Chinese to another encoding format, then use json_encode(), and finally use decoding to decode the json string. There is another way that is solved in the new version of PHP, as shown in the code below.

The following is a code example

<?php
  header("Content-type:text/html;charset=utf-8");
  $arrayName = array(&#39;city&#39; => &#39;广东&#39;,&#39;goods&#39;=>&#39;cookies&#39; );
  $arr = json_encode($arrayName);
  echo $arr."</br>";
  var_dump(json_decode($arr));
  echo "</br>";
  echo urldecode(json_encode(ch_json($arrayName)))."</br>";
/*
  需要php版本在5.4以上
  echo json_encode($arrayName,JSON_UNESCAPED_UNICODE);
*/
  function ch_json($arr){
    if(is_array($arr)){
      foreach ($arr as $key => $value) {
        $arr[urlencode($key)] = ch_json($value);
      }
    }else{
      return urlencode($arr);
    }
    return $arr;
  }
?>

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP’s Json Chinese processing solutions, please pay attention to the PHP Chinese website!

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