Introduction to the PHP Json_encode () function

Source: Internet
Author: User
Using the Json_encode () built-in function (Php > 5.2) in PHP makes it possible for data in PHP to be well communicated and used in other languages.
The function is to convert a numeric value into a JSON data storage format.

<?php  $arr = array         (            ' Name ' = ' Shia ',            ' age ' =>20         );  $jsonencode = Json_encode ($arr);  echo $jsonencode;  ? >

The results of the program run as follows:

{"Name": null, "Age": +}   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. PHP code  <?php  $array = array      (          ' title ' =>iconv (' gb2312 ', ' utf-8 ', ' Here is the Chinese title '),          ' body ' = > ' abcd ... '      );  echo Json_encode ($array);  ? >

The result of this program is:

JS Code

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

All Chinese in the

array are missing after Json_encode, or \u2353 are present. The

Workaround is to use the UrlEncode () function to handle the following, Json_encode all the contents of all arrays with UrlEncode (), and then convert the Json_encode () to a JSON string. Finally, I use UrlDecode () to turn the encoded Chinese back.

<?php/************************************************************** * * Use a specific function to manipulate all elements in an array * @param strin  G & $array The string to be processed * @param string $function the function to execute * @return Boolean $apply _to_keys_also is also applied to the key * @access Public * *************************************************************/function arrayrecursive (&$      Array, $function, $apply _to_keys_also = False) {static $recursive _counter = 0;      if (+ + $recursive _counter >) {die (' possible deep recursion attack '); } foreach ($array as $key = + $value) {if (Is_array ($value)) {arrayrecursive ($array [$key],          $function, $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 array to JSON string (Chinese-compatible) * @param array $arra Y the array to convert * @return the JSON string converted from 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:

JS Code

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

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