/***************************************************** **********
*
* 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)
{
static $recursive_counter = 0;
if (++$recursive_counter > 1000) {
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 (compatible with Chinese)
* @param array $array The array 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'=>'希亚',
'age'=>20,
'id'=>$_post['cid']
);
echo json($array);
/*********
{"name":"Xiya","age":"20"}
This tutorial is an example of php ajax returning json data, which uses ajax to accept json.php in real time The file is sent with the data request and processed.
* **********/
更多相关内容请关注PHP中文网(www.php.cn)!