Many friends may encounter problems with NULL or garbled characters when using the functions JSON_DECODE/JSON_ENCODE that comes with PHP to process Chinese content when using json data. Let me introduce to you why such problems occur.
Example
The code is as follows | Copy code | ||||||||||||||||||||||||||||
var_dump(json_decode($json)); var_dump(json_decode($json, true));
?> Output results
["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } Absolutely correct without any problems, then let’s test Chinese
This still sometimes causes problems. Then I found a way to use urlencode() to process all the contents of all arrays before json_encode, then use json_encode() to convert it into a json string, and finally use urldecode() Convert the encoded Chinese back. Write a function
* * Use a specific function to process all elements in the array * @param string &$array The string to be processed * @param string $function The function to be executed * @return boolean $apply_to_keys_also Whether to also apply to keys * @access public * *************************************************** ***********/ function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { 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)) { true http: //www.bkjia.com/PHPjc/632125.htmlTechArticleMany friends may encounter it when using json data using the function JSON_DECODE/JSON_ENCODE that comes with php to process Chinese content. NULL or garbled characters occur. Let me introduce to you why...
Previous article:php error Fatal error: Out of memory (allocated 262144)_PHP tutorial
Next article:Fatal error: Out of memory (allocated 786432) prompts solution_PHP tutorial
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
When adding sublime3 to compile system php, use the PHP toolbox, cmd php -v is useless
From 1970-01-01 08:00:00
0
0
0
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|