Home > Article > Backend Development > php ajax returns json data instance_PHP tutorial
This tutorial is an example of php ajax returning json data. It uses ajax to accept the data request sent by the json.php file in real time and processes it.
http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
PHP tutorial ajax returns web page special effects on data instance
json.php file
/***************************************************** *********** * 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":"Shia","age":"20"}
This tutorial is an example of php ajax returning json data. It uses ajax to accept the data request sent by the json.php file in real time and processes it.
Download address of this tutorial
http://down.php100.com/down/code/jquery/2010/0812/20181.html
* **********/
?>