频道栏目
首页 > 资讯 > 数组 > 正文

php把数组保存数据库程序代码

16-01-06        来源:[db:作者]  
收藏   我要投稿

我们在做缓存文件时经常会要把php代码或数组转换成字符串保存到数据库中,下面我来介绍两种把数组保存到数据库的方法。

方法一:用serialize写入,再用unserialize输出

serialize()就是将PHP中的变量如对象(object),数组(array)等等的值序列化为字符串后存储起来.序列化的字符串我们可以 存储在其他地方如数据库、Session、Cookie等,序列化的操作并不会丢失这些值的类型和结构。这样这些变量的数据就可以在PHP页面、甚至是不 同PHP程序间传递了。

而unserialize()就是把序列化的字符串转换回PHP的值。返回的是转换之后的值,可为 integer、float、string、array 或 object如果传递的字符串不可解序列化,则返回 FALSE,代码如下:

  1. class db { 
  2.  private $host; 
  3.  private $user; 
  4.  private $pwd; 
  5.  private $dbname; 
  6.  private $Mysqli; 
  7.  function __construct($host, $user, $pwd, $dbname) { 
  8.   $this->host = $host; 
  9.   $this->user = $user; 
  10.   $this->pwd = $pwd; 
  11.   $this->dbname = $dbname; 
  12.   $this->db(); 
  13.  } 
  14.  function db() { 
  15.   $this->mysqli = new mysqli ( $this->host, $this->user, $this->pwd, $this->dbname ); 
  16.  } 
  17.  function select() { 
  18.   $this->mysqli->query("SET CHARSET GBK"); 
  19.   $sql = "SELECT id,cname FROM hdw_channel"; 
  20.   $result = $this->mysqli 
  21.    ->query ( $sql ); 
  22.   $rows = array (); 
  23.   while ( $row = $result->fetch_assoc () ) { 
  24.    $rows [] = $row; 
  25.   } 
  26.   ECHO "
    "; 
  27.   print_r ( $rows ); 
  28.  } 
  29.  function __wakeup(){   //反序列化, 
  30.   $this->db(); 
  31.  } 
  32. $chanel = new db("localhost",'root','','hdcms'); 
  33. //$chanel->select(); 
  34. session_start(); 
  35. $_SESSION['channel_obj'] = serialize($chanel);   //将对象序列化,保存的是对象的属性,没有方法,所以要用__wakeup() 
  36.  
  37. class ren{ 
  38.  private $name; 
  39.  private $age; 
  40.  function __construct($name,$age){ 
  41.   $this->name =$name; 
  42.   $this->age = $age; 
  43.  } 
  44.  function show(){ 
  45.   echo "姓名是:{$this->name}  年龄是:{$this->age}"; 
  46.  } 
  47.  function __sleep(){ 
  48.   return array_keys(get_object_vars($this));  //或得数组里边的键名,序列化某些变量 
  49.  } 
  50. $zao = new ren("赵六",44); 
  51. echo serialize($zao);       //序列化(指定哪个变量序列化) 
  52. ==================================== 
  53. session_start(); 
  54. include '59.php'; 
  55. $channel_obj=unserialize($_SESSION['channel_obj']);  //反序列化类对象 
  56. $channel_obj->select();  //有了__wakeup方法才可以起作用 

方法二:用json_encode写入,再用json_decode输出

json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来,代码如下:

  1. /************************************************************** 
  2.  * 
  3.  * 使用特定function对数组中所有元素做处理 
  4.  * @param string &$array  要处理的字符串 
  5.  * @param string $function 要执行的函数 
  6.  * @return boolean $apply_to_keys_also  是否也应用到key上 
  7.  * @access public 
  8.  * 
  9.  *************************************************************/ 
  10. function arrayRecursive(&$array, $function, $apply_to_keys_also = false) 
  11.     static $recursive_counter = 0; 
  12.     if (++$recursive_counter > 1000) { 
  13.         die('possible deep recursion attack'); 
  14.     } 
  15.     foreach ($array as $key => $value) { 
  16.         if (is_array($value)) { 
  17.             arrayRecursive($array[$key], $function, $apply_to_keys_also); 
  18.         } else { 
  19.             $array[$key] = $function($value); 
  20.         } 
  21.  
  22.         if ($apply_to_keys_also && is_string($key)) { 
  23.             $new_key = $function($key); 
  24.             if ($new_key != $key) { 
  25.                 $array[$new_key] = $array[$key]; 
  26.                 unset($array[$key]); 
  27.             } 
  28.         } 
  29.     } 
  30.     $recursive_counter--; 
  31.  
  32. /************************************************************** 
  33.  * 
  34.  * 将数组转换为JSON字符串(兼容中文) 
  35.  * @param array $array  要转换的数组 
  36.  * @return string  转换得到的json字符串 
  37.  * @access public 
  38.  * 
  39.  *************************************************************/ 
  40. function JSON($array) { 
  41.  arrayRecursive($array, 'urlencode', true); 
  42.  $json = json_encode($array); 
  43.  return urldecode($json); 
  44. $array = array 
  45.        ( 
  46.           'Name'=>'希亚', 
  47.           'Age'=>20 
  48.        ); 
  49.  
  50. echo JSON($array); 
  51. ?>
相关TAG标签
上一篇:php中count 多维数组长度统计实现方法
下一篇:php中数组的搜索程序代码
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站