疑难答疑 PHP服务端返回Json问题
Jun 13, 2016 am 11:10 AM
我们需要在不断的经验积累中去完善我们的编程思路,灵活运用
后来看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问题的具体解决方法。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development

How do you parse and process HTML/XML in PHP?

PHP Program to Count Vowels in a String

The Key to Coding: Unlocking the Power of Python for Beginners

Java Made Simple: A Beginner's Guide to Programming Power

Create the Future: Java Programming for Absolute Beginners

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder
