php的json加密函数(php5.2以上免)

  1. function php_json_encode($arr)  
  2. {  
  3.     $json_str = "";  
  4.     if(is_array($arr))  
  5.     {  
  6.       $pure_array = true;  
  7.       $array_length = count($arr);  
  8.       for($i=0;$i<$array_length;$i++)  
  9.       {  
  10.         if(! isset($arr[$i]))  
  11.         {  
  12.           $pure_array = false;  
  13.           break;  
  14.         }  
  15.       }  
  16.       if($pure_array)  
  17.       {  
  18.         $json_str ="[";  
  19.         $temp = array();  
  20.         for($i=0;$i<$array_length;$i++)         
  21.         {  
  22.           $temp[] = sprintf("%s", php_json_encode($arr[$i]));  
  23.         }  
  24.         $json_str .= implode(",",$temp);  
  25.         $json_str .="]";  
  26.       }  
  27.       else  
  28.       {  
  29.         $json_str ="{";  
  30.         $temp = array();  
  31.         foreach($arr as $key => $value)  
  32.         {  
  33.           $temp[] = sprintf("/"%s/":%s"$key, php_json_encode($value));  
  34.         }  
  35.         $json_str .= implode(",",$temp);  
  36.         $json_str .="}";  
  37.       }  
  38.     }  
  39.     else  
  40.     {  
  41.       if(is_string($arr))  
  42.       {  
  43.         $json_str = "/"". json_encode_string($arr) . "/"";  
  44.       }  
  45.       else if(is_numeric($arr))  
  46.       {  
  47.         $json_str = $arr;  
  48.       }  
  49.       else  
  50.       {  
  51.         $json_str = "/"". json_encode_string($arr) . "/"";  
  52.       }  
  53.     }  
  54.     return $json_str;  
  55. }  

posted on 2012-03-21 09:40  风清扬令狐冲  阅读(686)  评论(0编辑  收藏  举报

导航