How to use php serialize() and unserialize()
A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.
Take for example this common problem:
How do I pass a PHP array to Javascript?
PHP and Javascript can only communicate via strings. You can pass the string "foo" very easily to Javascript. You can pass the number 1 very easily to Javascript. You can pass the boolean values true and false easily to Javascript. But how do you pass this array to Javascript?
Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 )
The answer is serialization. In case of PHP/Javascript, JSON is actually the better serialization format:
{ 1 : 'elem 1', 2 : 'elem 2', 3 : 'elem 3' }
Javascript can easily reverse this into an actual Javascript array.
This is just as valid a representation of the same data structure though:
a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}
But pretty much only PHP uses it, there's little support for this format anywhere else.
This is very common and well supported as well though:
<array>
<element key='1'>elem 1</element>
<element key='2'>elem 2</element>
<element key='3'>elem 3</element>
</array>
There are many situations where you need to pass complex data structures around as strings. Serialization, representing arbitrary data structures as strings, solves how to do this.
How to use php serialize() and unserialize()的更多相关文章
- serialize和unserialize函数
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性.1. serialize和u ...
- php函数serialize()与unserialize()
serialize()和unserialize()在php手册上的解释是: serialize — Generates a storable representation of a value ser ...
- php函数serialize()与unserialize() 数据序列化与反序列化
php函数serialize()与unserialize()说明及案例.想要将已序列化的字符串变回 PHP 的值,可使用unserialize().serialize()可处理除了resource之外 ...
- PHP多种序列化/反序列化的方法(serialize和unserialize函数)
serialize和unserialize函数 这两个是序列化和反序列化PHP中数据的常用函数. <?php $a = array('a' => 'Apple' ,'b' => 'b ...
- php 使用serialize() 和 unserialize() 让对象成超级变量
手册里面的原话和一些总结: php函数serialize()与unserialize()说明及案例.想要将已序列化的字符串变回 PHP 的值,可使用unserialize().serialize()可 ...
- golang实现php里的serialize()和unserialize()序列和反序列方法
Golang 实现 PHP里的 serialize() . unserialize() 安装 go get -u github.com/techleeone/gophp/serialize 用法 pa ...
- PHP之:序列化和反序列化-serialize()和unserialize()
撰写日期:2016-7-7 10:56:40 参考PHP在线手册(php.net):http://php.net/manual/zh/function.serialize.php 1.序列化 seri ...
- php数组序列化serialize与unserialize
$arr=array('1','2','3');echo serialize($arr); //序列化 a:3:{i:0;s:1:"1";i:1;s:1:"2" ...
- php中serialize、unserialize与json_encode、json_decode比较
性能比较 同一个变量编码或解码10000次,每个函数执行10000次所需时间 php5.2.13 json : 190 serialize : 257 json_encode : 0.08364200 ...
随机推荐
- Singleton模式C++实现
Singleton模式C++实现 Singleton是设计模式中比较简单的一个.园中的朋友们应该都很熟悉了.前段时间参加xxx外企的面试,和面试官讨论C++的时候正好写了一个.当时由于在有些地方考虑不 ...
- CentOS-6.5x64:VNC安装配置
1.安装软件前首先检查下系统是否已经安装了这个软件: rpm -qa tigervnc-server 2.根据前面命令的查询,显示系统还是没有安装VNC服务器端软件,那么我们就使用命令进行安装一下: ...
- 微软IE11浏览器的7大变化
微软IE11浏览器的7大变化 投递人 itwriter 发布于 2013-06-29 11:48 评论(9) 有1025人阅读 原文链接 [收藏] « » 微软很看重自己的 IE 浏览器,这款浏 ...
- Python学习入门基础教程(learning Python)--6.3 Python的list切片高级
上节"6.2 Python的list访问索引和切片"主要学习了Python下的List的访问技术:索引和切片的基础知识,这节将就List的索引index和切片Slice知识点做进一 ...
- ios-制作静态.a文件
一.制作静态库文件 /*静态库制作*/ // MakeA.h -(NSString*)testA; // MakeA.m -(NSString*)testA{ return @"静态库测试成 ...
- 在android里用ExpandableListView实现二层和三层列表
转载自http://www.cnblogs.com/nuliniaoboke/archive/2012/11/13/2767957.html 二层列表是直接用androidAPI中的Expandabl ...
- 用CATransform3D实现3D效果和制作简单3D动画
我们先来看下CATransform3D的头文件 struct CATransform3D { CGFloat m11, m12, m13, m14; CGFloat m21, m22, m23, m2 ...
- Android 动画的分类
分为三类: View Animation (补间动画 Tween动画) Drawable Animation(帧动画 Frame动画) Property Animation(android 3.0引入 ...
- 关于 百度 Ueditor 上传图片时 打开文件夹的延迟问题
在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/image ...
- 函数四种调用模式以及其中的this指向
第一种:函数直接执行模式 function add(a,b){ console.log(this); return a+b; } add(10,20)//this===window 第二种:对象方法的 ...