Json_encode UTF-8 Chinese garbled problem in PHP

Source: Internet
Author: User

Recently in the interface code used to Json_encode, found on the internet said Json_encode encoding set to UTF-8 Chinese will not garbled, verified that this method is really effective, but do not know why, the code after a period of time is not very useful. Here's your solution to the json_encode. There is a better way please share it!

The first type:

This simple to do a code conversion, Urlcode and then return the desired array

This is enough for my code.

Code to copy code as follows
public static function Encodeoperations ($array)

{

foreach ((array) $array as $key = + $value) {

if (Is_array ($value)) {

Encodeoperations ($array [$key]);

} else {

$array [$key] = UrlEncode (mb_convert_encoding ($value, ' UTF-8 ', ' GBK '));

}

}

return $array;

}

The second type:

This is seen on the internet, and then there is a comment that there will be an infinite loop problem, but this is obviously very comprehensive, and then I do have after the test, posted here for reference only

Code to copy code as follows

/**************************************************************

*

* Processing of all elements in an array using a specific function

* @param string & $array strings to be processed

* After @param string $tocode encoded

* @param string $oldcode before encoding

* @param string $function the function to execute

* @return Boolean $apply _to_keys_also is also applied to the key

* @return The array $array is also applied to the key

* @access Public

*

*************************************************************/

Function Encodeoperations (& $array, $function, $tocode =false, $oldcode =false, $apply _to_keys_also = False)

{

foreach ($array as $key = = $value) {
(www.111cn.net)
if (Is_array ($value)) {

Encodeoperations ($array [$key], $function, $apply _to_keys_also);

} else {

if ($tocode && $oldcode) {

if (function_exists (mb_convert_encoding)) {

$value = mb_convert_encoding ($value, $tocode, $oldcode);

}else{

return "error";

}

}

$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]);

}

}

}

return $array;

}

/****from:http://www.111cn.net/phper/31/66729.htm**********************************************************

*

* Convert Array to JSON string (Chinese compatible)

* @param array $array to convert

* JSON string to be converted @return string

* @access Public

*

*************************************************************/

function JSON ($array) {

Arrayrecursive ($array, ' UrlEncode ', true);

$json = Json_encode ($array);

Return UrlDecode ($json);

}

If the return to Chinese is null, we need to convert the character to uft8 or use UrlEncode to compile the call first.


Json_encode UTF-8 Chinese garbled problem in PHP

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.