1分钟部署网站📞AI智能客服,大模型训练自有数据,简单好用,有效降低客服成本 广告
## serialize 串行化 >[info] 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)** :反串行化 >[danger]反串行话的时候,类必须存在(否则是不完整的类) * * * ``` 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; ``` 例子2: ``` $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" } ```