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 ...
随机推荐
- CentOS下Eclipse的安装教程
CentOS下Eclipse的安装教程 据了解,在Linux下的Java开发很多时候都比较喜欢使用vim + 插件,反而很少使用Eclipse,但是我是第一次使用Linux来进行Java编程,就什么都 ...
- MapXtrem + Asp.net 地图随窗体改变大小
在B/S框架下,MapXtreme都是基于图片的,也就是说在客户端显示的地图实际上都是一张图片. 地图控件 <cc1:MapControl ID="/> 在调试模式下,编译后的地 ...
- 你不知道的Eclipse用法:使用Allocation tracker跟踪Android应用内存分配
Android Tools中的DDMS带有一个很不错的跟踪内存分配的工具Allocation tracker.通过Alloction tracker,不仅知道分配了哪类对象,还可以知道在哪个线程.哪个 ...
- jvm加载class原理
“委托机制”是指先委托父类装载器寻找目标类,只有在找不到的情况下才从自己的路径中查找并载入.这一点是从安全的方面考虑的,试想一下如果有人写了一个恶意的基础类(如java.lang.String)并加载 ...
- 第 1 章 Node.js 介绍
本章内容包括: 什么是 Node.js 框架,为什么要用 Node.js 框架,使用 Node.js 框架能够解决什么问题,在哪些场合下应该考虑使用 Node.js 框架. 如何下载 Node.js ...
- CMake学习
CMake学习 本篇分享一下有关CMake的一些学习心得以及相关使用. 作者:AlphaGL.版权所有,欢迎保留原文链接进行转载 :) 本文目录如下: 1.CMake介绍 2.CMake安装与使用 2 ...
- Dubbo源码学习--服务是如何发布的
相关文章: Dubbo源码学习--服务是如何发布的 Dubbo源码学习--服务是如何引用的 ServiceBean ServiceBean 实现ApplicationListener接口监听Conte ...
- IONIC之简易购物车
HTML <div ng-app="app"> <div class="l-header"> <div class="l ...
- GOPS2017全球运维大会深圳站 出席嘉宾盘点!
去年,GOPS全球运维大会在深圳出发,当时门票提前几周收盘,2017年,承载着运维人的期望,GOPS全球运维大会再次来到了深圳.第六届GOPS2017全球运维大会深圳站(本次)将于2017年4月21日 ...
- Pos终端中的主密钥、工作密钥、pin密钥、mac密钥
下面介绍一下加密机中最主要的几种密钥: 1.加密机主密钥(MK) 加密机主密钥(Master Key - MK)是存入在HSM机内的由三个成分合成的一对最上层密钥.在HSM机器以外的地方不会以明文形式 ...