• <i id='5gfEa'><tr id='5gfEa'><dt id='5gfEa'><q id='5gfEa'><span id='5gfEa'><b id='5gfEa'><form id='5gfEa'><ins id='5gfEa'></ins><ul id='5gfEa'></ul><sub id='5gfEa'></sub></form><legend id='5gfEa'></legend><bdo id='5gfEa'><pre id='5gfEa'><center id='5gfEa'></center></pre></bdo></b><th id='5gfEa'></th></span></q></dt></tr></i><div id='5gfEa'><tfoot id='5gfEa'></tfoot><dl id='5gfEa'><fieldset id='5gfEa'></fieldset></dl></div>
    <legend id='5gfEa'><style id='5gfEa'><dir id='5gfEa'><q id='5gfEa'></q></dir></style></legend>

        <tfoot id='5gfEa'></tfoot>

          <bdo id='5gfEa'></bdo><ul id='5gfEa'></ul>

        <small id='5gfEa'></small><noframes id='5gfEa'>

        php中serialize与unserialize用法实例代码

        时间:2024-10-18

        <i id='vsaWB'><tr id='vsaWB'><dt id='vsaWB'><q id='vsaWB'><span id='vsaWB'><b id='vsaWB'><form id='vsaWB'><ins id='vsaWB'></ins><ul id='vsaWB'></ul><sub id='vsaWB'></sub></form><legend id='vsaWB'></legend><bdo id='vsaWB'><pre id='vsaWB'><center id='vsaWB'></center></pre></bdo></b><th id='vsaWB'></th></span></q></dt></tr></i><div id='vsaWB'><tfoot id='vsaWB'></tfoot><dl id='vsaWB'><fieldset id='vsaWB'></fieldset></dl></div>
          <tbody id='vsaWB'></tbody>

        <small id='vsaWB'></small><noframes id='vsaWB'>

            <bdo id='vsaWB'></bdo><ul id='vsaWB'></ul>

              <tfoot id='vsaWB'></tfoot>
            1. <legend id='vsaWB'><style id='vsaWB'><dir id='vsaWB'><q id='vsaWB'></q></dir></style></legend>
                • serialize():将对象转化为字符串保存、unserialize():将字符串反转为对象(对应的类也必须包含)
                  serialize --  产生一个可存储的值的表示;返回字符串,此字符串包含了表示value的字节流,可以存储于任何地方
                  /*  
                   串行化(序列化)  
                  */
                  class Person  
                  {  
                   public $name;  
                   public $age;  
                   public $sex;  
                   public $sanwei;
                  
                   public function __construct($name, $age, $sex, $sanwei)  
                   {  
                    $this->name = $name;  
                    $this->age = $age;  
                    $this->sex = $sex;  
                    $this->sanwei = $sanwei;  
                   }  
                  }
                  
                  // 实例化Person类  
                  $p = new Person('凤姐', 20, '女', '100-70-100');  
                  var_dump($p);  
                  $str = serialize($p); // O:6:"Person":3:{s:4:"name";s:6:"凤姐";s:3:"age";i:20;s:3:"sex";s:3:"女";}
                  
                  file_put_contents('9.txt', $str);  
                  /*  
                   O:object  
                   6:类名的字符个数  
                   Person:类名  
                   3:三个属性  
                   s:string 数据类型   
                   i:int 数据类型  
                  */
                  unserialize($param) :反串行化
                  class Person  
                  {  
                      public $name;  
                      public $age;  
                      public $sex;  
                      public $sanwei;
                  
                      public function __construct($name, $age, $sex, $sanwei)  
                      {  
                          $this->name = $name;  
                          $this->age = $age;  
                          $this->sex = $sex;  
                          $this->sanwei = $sanwei;  
                      }  
                  }
                  
                  // 1.获取9.txt文本的内容  
                  $str = file\_get\_contents('9.txt');//O:6:"Person":4:{s:4:"name";s:6:"凤姐";s:3:"age";i:20;s:3:"sex";s:3:"女";s:6:"sanwei";s:10:"100-70-100";}
                  
                  // 2.反串行化  
                  $obj = unserialize($str);
                  
                  echo $obj->name;  
                  echo $obj->age;  
                  echo $obj->sex;
                  例子:
                  $intParam = 123;
                  $stringParam= 'I love the world';  
                  $arrayParam = array(  
                      "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"),  
                      "numbers" => array(1, 2, 3, 4, 5, 6),  
                      "holes" => array("first", 5 => "second", "third")  
                  ); 
                  
                  class Object  
                  {  
                      public $memu = "index";  
                  }  
                  $objectParam = new Object();  
                  
                  print 'serialize($intParam) 输出的值:'.serialize($intParam);
                  print 'serialize($stringParam) 输出的值:'.serialize($stringParam);
                  print 'serialize($arrayParam) 输出的值:'.serialize($c=$arrayParam);  
                  print 'serialize($objectParam) 输出的值:'.serialize($d=$objectParam);
                  print 'unserialize(serialize($intParam)) 的结果是:'.unserialize(serialize($intParam));
                  print 'unserialize(serialize($stringParam)) 的结果是:'.unserialize(serialize($stringParam));
                  
                  print 'unserialize(serialize($arrayParam)) 的结果是:'.unserialize(serialize($arrayParam));
                  var_dump(unserialize(serialize($arrayParam)));  
                  
                  print 'unserialize(serialize($objectParam)) 的结果是:';
                  var_dump(unserialize(serialize($objectParam)));
                  结果代码:
                  serialize($intParam) 输出的值:i:123;
                  serialize($stringParam) 输出的值:s:16:"I love the world";
                  serialize($arrayParam) 输出的值:a:3:{s:6:"fruits";a:3:{s:1:"a";s:6:"orange";s:1:"b";s:6:"banana";s:1:"c";s:5:"apple";}s:7:"numbers";a:6:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;}s:5:"holes";a:3:{i:0;s:5:"first";i:5;s:6:"second";i:6;s:5:"third";}}
                  serialize($objectParam) 输出的值:O:6:"Object":1:{s:4:"memu";s:5:"index";}
                  unserialize(serialize($intParam)) 的结果是:123
                  unserialize(serialize($stringParam)) 的结果是:I love the world
                  
                  unserialize(serialize($arrayParam)) 的结果是:Arrayarray(3) {
                    ["fruits"]=>
                    array(3) {
                      ["a"]=>
                      string(6) "orange"
                      ["b"]=>
                      string(6) "banana"
                      ["c"]=>
                      string(5) "apple"
                    }
                    ["numbers"]=>
                    array(6) {
                      [0]=>
                      int(1)
                      [1]=>
                      int(2)
                      [2]=>
                      int(3)
                      [3]=>
                      int(4)
                      [4]=>
                      int(5)
                      [5]=>
                      int(6)
                    }
                    ["holes"]=>
                    array(3) {
                      [0]=>
                      string(5) "first"
                      [5]=>
                      string(6) "second"
                      [6]=>
                      string(5) "third"
                    }
                  }
                  
                  unserialize(serialize($objectParam)) 的结果是:object(Object)#2 (1) {
                    ["memu"]=>
                    string(5) "index"
                  }
                  
                   
                  上一篇:php读写json文件及修改json方法 下一篇:没有了

                  相关文章

                    <legend id='g2MsI'><style id='g2MsI'><dir id='g2MsI'><q id='g2MsI'></q></dir></style></legend>
                    <tfoot id='g2MsI'></tfoot>

                        <bdo id='g2MsI'></bdo><ul id='g2MsI'></ul>

                    1. <i id='g2MsI'><tr id='g2MsI'><dt id='g2MsI'><q id='g2MsI'><span id='g2MsI'><b id='g2MsI'><form id='g2MsI'><ins id='g2MsI'></ins><ul id='g2MsI'></ul><sub id='g2MsI'></sub></form><legend id='g2MsI'></legend><bdo id='g2MsI'><pre id='g2MsI'><center id='g2MsI'></center></pre></bdo></b><th id='g2MsI'></th></span></q></dt></tr></i><div id='g2MsI'><tfoot id='g2MsI'></tfoot><dl id='g2MsI'><fieldset id='g2MsI'></fieldset></dl></div>
                    2. <small id='g2MsI'></small><noframes id='g2MsI'>