Home > Backend Development > PHP Tutorial > How to process Json Chinese in php

How to process Json Chinese in php

怪我咯
Release: 2023-03-12 14:44:02
Original
1494 people have browsed it

This article mainly introduces PHP's Json Chinese processing solution, involving related operating skills of PHP encoding conversion, and has certain reference value. Friends in need can refer to it

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

Json is now a widely used format for transmitting strings. Compared with xml, it is simpler to understand and more convenient to operate. There are only two functions under php, json_encode() AND json_deconde(). However, json's support for Chinese is not very good. If you use json_encode() to process such as 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;
  }
?>
Copy after login

The above is the detailed content of How to process Json Chinese in php. For more information, please follow other related articles on the PHP Chinese website!

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