PHP json_encode () output garbled problem

Source: Internet
Author: User

{"title": "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898", "Body": "ABCD ..."}

All the Chinese in the array have disappeared after the Json_encode, or appear \u2353.

The workaround is to use the UrlEncode () function to handle the following, before Json_encode, all the contents of all arrays are processed with UrlEncode (), and then converted to a JSON string with Json_encode (), and then UrlDecode () Turn the encoded Chinese back.

<?php/************************************************************** * * Handle all elements in an array with a specific function * @param string  & $array string to be processed * @param string $function function to execute * @return Boolean $apply _to_keys_also is also applied to key * @access public * /function arrayrecursive (& $array, $function, $  Apply_to_keys_also = False) {static $recursive _counter =; if (+ + $recursive _counter >) {die (' possible deep recursion Attack '); } foreach ($array as $key = + $value) {if (Is_array ($value)) {arrayrecursive ($array [$key], $function, $apply _to_key  S_ALSO);  } else {$array [$key] = $function ($value);   } if ($apply _to_keys_also && is_string ($key)) {$new _key = $function ($key);    if ($new _key! = $key) {$array [$new _key] = $array [$key];   Unset ($array [$key]); }}} $recursive _counter--;} /************************************************************** * * Convert an array to a JSON string (Chinese language compatible) * @param array $array the arrays to be converted * @ return string The converted JSON String * @access public * *************************************************************/function JSON ($array) { Arrayrecursive ($array, ' UrlEncode ', true); $json = Json_encode ($array); Return UrlDecode ($json);} $array = Array (' Name ' = ' Shia ', ' age ' =); echo JSON ($array); >

So Chinese will be able to output the normal

PHP json_encode () output garbled problem

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.