search
Homephp教程php手册PHP的serialize序列化数据以及JSON格式化数据分析

这篇文章的内容是PHP的serialize序列化数据以及JSON格式化数据分析,需要的朋友可以参考下

PHP的serialize是将变量序列化,返回一个具有变量类型和结构的字符串表达式,,而JSON则是一种更轻、更友好的用于接口(AJAX、REST等)数据交换的格式。 其实两者都是以一种字符串的方式来体现一种数据结构。那么他们之间又有什么区别呢?
serialize序列化
我们在一些老的WEB系统中可能会看到在数据库或在文本文件中存储着一大串貌似有着特殊含义的字符串文本内容。我们仔细看会发现它具有数据类型和结构等信息,但是它并不容易人工阅读,它只适合PHP程序读取。PHP的serialize将数组进行了序列化并存储。我们假设有这样一个数组:

$arr = array( "0" => array( "gameName" => "德乙", "homeName" => "比勒费尔德", "guestName" => "不伦瑞克", "endTime" => "2015-08-21" ), "1" => array( "gameName" => "英超", "homeName" => "水晶宫", "guestName" => "阿斯顿维拉", "endTime" => "2015-08-22" ) );

我们要将这个数组内容存储在数据库或者文本文件中,以便其他地方读取。

$serialize = serialize($arr); echo $serialize;

我们使用PHP的serialize将数组进行序列化,输出以下结果:

a:2:{i:0;a:4:{s:8:"gameName";s:6:"德乙";s:8:"homeName";s:15:"比勒费尔德";s:9:"guestName";s:12:"不伦瑞克";s:7:"endTime";s:10:"2015-08-21";}i:1;a:4:{s:8:"gameName";s:6:"英超";s:8:"homeName";s:9:"水晶宫";s:9:"guestName";s:15:"阿斯顿维拉";s:7:"endTime";s:10:"2015-08-22";}}

以上输出的结果看上去比较复杂,其实也很简单,它说明的就是一些数据类型和结构。
a:2说明这是个有两个元素的数组(array);
i:0指序列索引;
a:4指有4个字段
s:8:"gameName"说明这是有8个字符的字符串(string)
实际开发中我们只将序列化后的数据进行存储,并不要去关心存储的格式和以及字段意思。如果要将序列化后的数据恢复成数组,则使用unserialize()函数即可。

print_r(unserialize($serialize));

以上代码可以打印出数组。
JSON数据解析
我们知道,PHP操作JSON可以使用json_encode()和json_decode()两个函数。json_encode()可以将数组转换成json格式的文本数据,它方便存储和读取,而json_decode()可以直接将json数据转换成数组,方便调用。

$jsonencode = json_encode($arr); echo $jsonencode;

输出:

[{"gameName":"\u5fb7\u4e59","homeName":"\u6bd4\u52d2\u8d39\u5c14\u5fb7","guestName":"\u4e0d\u4f26\u745e\u514b","endTime":"2015-08-21"},{"gameName":"\u82f1\u8d85","homeName":"\u6c34\u6676\u5bab","guestName":"\u963f\u65af\u987f\u7ef4\u62c9","endTime":"2015-08-22"}]

很显然,使用JSON后,数据空间要比serialize少,输出的结果中中文字符串经过编码,仔细看是键值对应,便于人工识别,而且关键是JSON格式的数据方便其他语种读取并识别,所以有人说它是XML替代品。JSON格式的数据能和WEB前端JS完成异步交互过程。如果要将json还原成数组,可以使用json_decode()函数。
print_r(json_decode($jsonencode,true));
关于JSON的应用有兴趣的同学可以参考本站文章:PHP中JSON的应用
总结
PHP的serialize将数组序列化后是便于存储,而JSON格式的数据不仅便于存储还能跟其他语言如javascript读取。它们在性能上可能会有细微区别,如果前后端交互使用比较多的话建议使用JSON,结合PHP、Javascript、JSON以及Ajax就可以完成强大的数据交互功能。

关于PHP的serialize序列化数据以及JSON格式化数据详细分析就这么多,如果想了解更多的内容请继续关注。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software