Home > Article > Backend Development > How to correctly use the PHP json_encode function for Chinese conversion_PHP tutorial
What we will bring to you today is the solution to the problem when the PHP json_encode function handles Chinese conversion. json is a very good data structure and is now widely used in network data transmission.
json_encode and json_decode
There are many related information on the Internet about the specific usage of these two functions. article, this article mainly introduces the solution to the problem that Chinese cannot be converted when using json_encode. This article assumes that the encoding used in the file is gb2312;
Write the required array first
If you use the PHP json_encode function directly
<ol class="dp-xml"> <li class="alt"> <span class="tag"><</span> ?PHP </li><li><span>echo json_encode($json); </span></li><li class="alt"><span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
The result is:
<ol class="dp-xml"> <li class="alt"> <span class="tag"><</span> ?PHP </li><li><span>[{"id":"13","name":null}<br />,{"id":"13","name":null}] </span></li><li class="alt"><span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
You can see that all Chinese characters that are not escaped are null. This is because json only escapes encoding, so the above statement should convert the encoding first
<ol class="dp-xml"> <li class="alt"> <span class="tag"><</span> ?PHP </li><li><span>foreach ($ajax as $</span><span class="attribute">key</span><span>=</span><span class="tag">></span><span>$val) </span> </li> <li class="alt"><span>{ </span></li> <li><span>$ajax[$key]['name'] = <br>urlencode($val['name']); </span></li> <li class="alt"><span>} </span></li> <li><span>echo json_encode($json); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
Client js code
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> </span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span> </span></span></li> <li><span>function getsort(obj) </span></li> <li class="alt"><span>{ </span></li> <li><span>$.ajax( </span></li> <li class="alt"><span>{ </span></li> <li><span>type : "GET", </span></li> <li class="alt"> <span>url : "</span><span class="tag"><</span><span> ?=$this-</span><span class="tag">></span><span>baseUrl</span><span class="tag">?></span><span>/index/getajax", </span> </li> <li> <span>data : "</span><span class="attribute">c</span><span>=" obj.value, </span> </li> <li class="alt"><span>success : function(json) </span></li> <li><span>{ </span></li> <li class="alt"> <span>var </span><span class="attribute">json</span><span>=</span><span class="attribute-value">eval</span><span>(json); </span> </li> <li> <span>var </span><span class="attribute">html</span><span> = </span><span class="attribute-value">'< select>'</span><span>; </span> </li> <li class="alt"><span>$.each(json, function(k) </span></li> <li><span>{ </span></li> <li class="alt"> <span class="attribute">html</span><span> = </span><span class="attribute-value">'< option value="'</span><span> <br />json[k]['id'] '"</span><span class="tag">></span><span>' <br>decodeURI(json[k]['name']) '</span><span class="tag"><</span><span> /option</span><span class="tag">></span><span>'; </span> </li> <li><span>}); </span></li> <li class="alt"> <span class="attribute">html</span><span> =</span><span class="attribute-value">"< /select>"</span><span>; </span> </li> <li><span>$('#sort').html(html); </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span>) </span></li> <li><span>} </span></li> <li class="alt"> <span class="tag"><</span><span> /script</span><span class="tag">></span><span> </span> </li> </ol>
Using the above code js will report an error saying that the encoding does not comply The reason for standard
is because decodeURI in js only supports utf8 transcoding. Therefore, the code of the PHP json_encode function should be the following code
<ol class="dp-xml"> <li class="alt"> <span class="tag"><</span> ?PHP </li><li><span>foreach ($ajax as $</span><span class="attribute">key</span><span>=</span><span class="tag">></span><span>$val) </span> </li> <li class="alt"><span>{ </span></li> <li><span>$ajax[$key]['name'] = <br>urlencode(iconv('gb2312',<br>'utf-8',$val['name'])); </span></li> <li class="alt"><span>} </span></li> <li><span>echo json_encode($json); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> <li><span> </span></li> </ol>
The above is using PHP json_encode Solutions to problems encountered in actual operation of functions.