About the PHP Json_encode () function _php Tutorial

Source: Internet
Author: User
Tags urlencode
Using the Json_encode () built-in function in PHP (PHP > 5.2) can be used in PHP to communicate well with other languages and use it.

The function is to convert a numeric value into a JSON data storage format.

 
  ' Shia ',          ' age ' =>20       ); $jsonencode = Json_encode ($arr); Echo $jsonencode; >

The results of the program run as follows:

{"Name": null, "Age": 20}

Json_encode function in Chinese is encoded as NULL, Google a bit, very simple, in order to closely integrate with the front, Json only support utf-8 encoding, I think is the front-end Javascript is also the reason for Utf-8.

 
  Iconv (' gb2312 ', ' utf-8 ', ' Here is the Chinese title '), ' body ' = ' abcd ... '); Echo Json_encode ($array);? >

The result of this program is:

{"title": "u8fd9u91ccu662fu4e2du6587u6807u9898", "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.

  (' Possible deep recursion attack '); } foreach ($array as $key = + $value) {if (Is_array ($value)) {arrayrecursive ($array [$key], $funct        Ion, $apply _to_keys_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) * @paramarray $array the array to convert *@ The JSON string converted by the return string * @access public * *************************************************************/function JSON ($array) {arrayrecursive ($array, ' UrlEncode ', true), $json = Json_encode ($array); return UrlDecode ($json);} $array = Array (' Name ' = ' Shia ', ' age ' =>20); Echo JSON ($array); >

This success, the results of the operation are as follows:

{"Name": "Shia", "Age": "20"}

http://www.bkjia.com/PHPjc/752519.html www.bkjia.com true http://www.bkjia.com/PHPjc/752519.html techarticle using the Json_encode () built-in function in PHP (PHP > 5.2) can be used in PHP to communicate well with other languages and use it. The function is to convert the value into ...

  • 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.