Home > Article > Backend Development > php json supports Chinese
支持中文的 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);
}
?>