php序列化函数serialize() 和 unserialize() 与原生函数
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了php序列化函数serialize() 和 unserialize() 与php原生序列化方法对比,有需要的小伙伴可以参考下。
php中有格式化字符串并转换成数组或对象的好方法,即序列化处理。
有两种序列化变量的方法。
以下示例,使用 serialize() 和 unserialize() 函数:
// a plex array $myvar = array( 'hello', 42, array(1,'two'), 'apple' ); // convert to a string $string = serialize($myvar); echo $string; /* prints a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:0;i:1;i:1;s:3:"two";}i:3;s:5:"apple";} */ // you can reproduce the original variable $newvar = unserialize($string); print_r($newvar); /* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */
这是原生的 PHP 序列化方法。
然而,由于 JSON 近年来大受欢迎,PHP5.2 中已经加入了对 JSON 格式的支持。
现在你可以使用 json_encode() 和 json_decode() 函数:
// a plex array $myvar = array( 'hello', 42, array(1,'two'), 'apple' ); // convert to a string $string = json_encode($myvar); echo $string; /* prints ["hello",42,[1,"two"],"apple"] */ // you can reproduce the original variable $newvar = json_decode($string); print_r($newvar); /* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */
这将更为行之有效,尤其与 JavaScript 等许多其他语言兼容。
注意:对于复杂的对象,某些信息可能会丢失。
以上所述就是本文的全部内容了,希望大家能够喜欢。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程