Home  >  Article  >  php教程  >  PHP实例代码:模仿百度搜索时输入内容带提示

PHP实例代码:模仿百度搜索时输入内容带提示

WBOY
WBOYOriginal
2016-06-21 08:50:331328browse

自己用PHP + js模仿的百度搜索时输入内容带提示的功能,运行的话需要PHP环境。也算是自己的一个练笔题吧,和大家分享一下

        $aName = array("王成","王瑞","王祥","何武昌","何睿","何好","马兴","马成栋","张相","张祥");
        $aId = array(88801,11144,2345,9023,12415,88021,11145,2344,11145,0376);
        foreach($aName as $k =>$val)
        {
                $json[$k] = array(
                        'id' => $aId[$k],
                        'name' => $val
                );       
        }

/**************************************************************
*
*  使用特定function对数组中所有元素做处理
*  @param  string  &$array     要处理的字符串
*  @param  string  $function   要执行的函数
*  @return boolean $apply_to_keys_also     是否也应用到key上
*  @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--;
}
/**************************************************************
*
*  将数组转换为JSON字符串(兼容中文)
*  @param  array   $array      要转换的数组
*  @return string      转换得到的json字符串
*  @access public
*
*************************************************************/
function JSON($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);
}
?>



       
          


       
           
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