---
title: "PHP 序列化和反序列化"
author: "onestopweb"
date: "2018-01-25T18:52:22Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2018-01-25T18:52:22Z"
id: 2409085
url: "https://onestopweb.iteye.com/blog/2409085"
---

```
<?php
header('Content-Type: text/html; charset=utf-8');

//序列化和反序列化
//你是否会把一个比较复杂的数据结构存到数据库或是文件中？你并不需要自己去写自己的算法。
//PHP早已为你做好了，其提供了两个函数：serialize() 和 unserialize():

$str = array('chaoyi','onestopweb.iteye.com','阅谁问君诵，水落清香浮。');
//序列化
$str2 = serialize($str);
echo $str2.PHP_EOL;
//反序列化
$str3 = unserialize($str2);
var_dump($str3);
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0128/6786/7f2a7f1a-4a71-38b2-af19-cd50429c60dc.png)