This article mainly introduces PHP Json_encode () functions in detail, interested in the reference of friends, I hope to help you.
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.
<?php$arr = array ( ' Name ' = ' Shia ', ' age ' = ); $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-encoding, I think is the front-end Javascript is also the reason for utf-.
<?php$array = Array (' title ' =>iconv (' GB ', ' utf-', ' Here is the Chinese title '), ' body ' = ' abcd ... '); echo Json_encode ($array);? >
The result of this program is:
{"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 process the following, Json_encode all the contents of all arrays with UrlEncode (), and then convert the Json_encode () to a JSON string, and then use the UrlDecode ( ) To 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); >
This success, the results of the operation are as follows:
{"Name": "Shia", "Age": "20"}
Summary: The above is the entire content of this article, I hope to be able to help you learn.