关于php数组序列化和反序列化
函数的使用
1 serialize 数组序列化
2 unserialize 反数组序列化
在开发中,存储数组到mysql里,需要把数组序列化,转换之后才能存储到数据库里
先看看序列化的例子
class CC { public $data; private $pass; public function __construct($data, $pass) { $this->data = $data; $this->pass = $pass; } } $number = 34; $str = 'uusama'; $bool = true; $null = NULL; $arr = array('a' => 1, 'b' => 2); $cc = new CC('uu', true); var_dump(serialize($number)); var_dump(serialize($str)); var_dump(serialize($bool)); var_dump(serialize($null)); var_dump(serialize($arr)); var_dump(serialize($cc));
结果为:
string(5) "i:34;" string(13) "s:6:"uusama";" string(4) "b:1;" string(2) "N;" string(30) "a:2:{s:1:"a";i:1;s:1:"b";i:2;}" string(52) "O:2:"CC":2:{s:4:"data";s:2:"uu";s:8:" CC pass";b:1;}"