序列化是将系统对象转换成字符串的过程,反序列化则是将字符串再转换成系统对象的过程。序列化后的字符串可以很方便的保存到操作系统文件,数据库,或者通过网络传输到其它电脑。PHP提供了实现序列化的方法serialize和unserialize。
请将下面的代码保存到一个PHP文件中:
<?php
$a = array(
"name" => "amonest",
"age" => 30,
"married" => true,
"attributes" => array(
"money" => 3000.1,
"height" => 170,
"weight" => 85.2
)
);
$s = serialize($a);
var_dump($s);
$r = unserialize($s);
var_dump($r);
上面代码执行结果如下:
string(233) "a:4:{s:4:"name";s:7:"amonest";s:3:"age";i:30;s:7:"married";b:1;s:10:"attributes";a:3:{s:5:"money";d:3000.09999999999990905052982270717620849609375;s:6:"height";i:170;s:6:"weight";d:85.2000000000000028421709430404007434844970703125;}}"
array(4) {
["name"]=>
string(7) "amonest"
["age"]=>
int(30)
["married"]=>
bool(true)
["attributes"]=>
array(3) {
["money"]=>
float(3000.1)
["height"]=>
int(170)
["weight"]=>
float(85.2)
}
}
浙公网安备 33010602011771号