我们需要在不断的经验积累中去完善我们的编程思路,灵活运用
后来看json_encode的文档才知道,它只能编码UTF-8的字符。这个问题就是返回的字符是GB2312,怎么变为UTF-8,并且最好是将整个数组进行UTF-8的转换。经过搜索有以下代码解决PHP服务端返回Json问题:
- function encodeUTF8($array)
- {
-
foreach($array as $key=>$value)
- {
- if(!is_array($value))
- {
- $array[$key]=mb_convert
_encoding($value,"UTF-8","GBK"); - }
- else
- {
- encodeUTF8($array[$key]);
- }
- }
- return $array;
- }
每次返回结果时:
<ol class="dp-xml"><li class="alt"><span><span>echo '{"total":"'.$rscount.'<br>","results":'.json_encode<br>(encodeUTF8($result)).'}'; </span></span></li></ol>
以上这段代码就是PHP服务端返回Json问题的具体解决方法。
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