Home  >  Article  >  Backend Development  >  php json supports Chinese

php json supports Chinese

巴扎黑
巴扎黑Original
2016-11-24 10:58:161222browse

支持中文的 php json 函数

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

*

* Use a specific function to process all elements in the array

* @param string &$array The string to be processed

* @param string $function to be executed Function

* @return boolean $apply_to_keys_also Whether it is also applied to key

* @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)) {

            $new_key = $function($key);

            if ($new_key != $key) {

                $array[$new_key] = $array[$key];

                unset($array[$key]);

            }

        }

    }

}

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

*

* Convert array to JSON string (compatible with Chinese)

* @param array $array The array to be converted

* @return string The converted json character String

* @access public

*

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

function JSON($array) {

arrayRecursive($array, 'urlencode', true);

$json = json_encode($array);

return urldecode($json);

}


?>


Statement:
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