Home  >  Article  >  Web Front-end  >  php ajax returns json data instance

php ajax returns json data instance

黄舟
黄舟Original
2016-12-14 10:13:471456browse





php教程 ajax返回 网页特效on数据实例



       
   



json.php 文件

/***************************************************** **********
*
* 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)!

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