FAQs: PHP Server return Json _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Q & A: The PHP server returns Json. We need to improve our programming ideas in the accumulation of experience, flexible use of the json_encode documentation to see, it can only encode the characters of the UTF-8. This problem is returned. we need to improve our programming ideas through continuous experience and use them flexibly.

After looking at the json_encode document to know, it can only encode the characters of the UTF-8. This problem is how the returned characters are GB2312, and it is best to convert the entire array into a UTF-8. Search for the following code to solve the Json returned by the PHP server:

  1. Function encodeUTF8 ($ array)
  2. {
  3. Foreach ($ array as $ key =>$ Value)
  4. {
  5. If (! Is_array ($ value ))
  6. {
  7. $ Array [$ key] = mb_convert
    _ Encoding ($ value, "UTF-8", "GBK ");
  8. }
  9. Else
  10. {
  11. EncodeUTF8 ($ array [$ key]);
  12. }
  13. }
  14. Return $ array;
  15. }

Each time a result is returned:

 
  1. echo '{"total":"'.$rscount.'
    ","results":'.json_encode
    (encodeUTF8($result)).'}';

The above code is the specific solution to the Json problem returned by the PHP server.


After coding, let's see the json_encode document to know that it can only encode characters of the UTF-8. This problem is returned...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.