Home  >  Article  >  Backend Development  >  php ajax returns json data instance_PHP tutorial

php ajax returns json data instance_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:01:21921browse

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

* **********/
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445440.htmlTechArticle本教程是一款php ajax返回 json数据实例哦,就是利用ajax实时的接受json.php文件发送的数据请求,并且进行了处理。 http://www.w3.org/tr/xhtml1/dtd/...
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