如何使用 php serialize() 和 unserialize() [英] How to use php serialize() and unserialize()

查看:33
本文介绍了如何使用 php serialize() 和 unserialize()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很基本.

关于serialize()unserialize() 在php 中的确切含义,我没有找到任何示例来满足我的需求?他们只是举了一个例子 - 序列化一个数组并以无法解释的格式显示输出.通过他们的行话很难理解基本概念.

I did not find any example to meet my needs as to what exactly serialize() and unserialize() mean in php? They just give an example - serialize an array and show an output in an unexplained format. It is really hard to understand the basic concept going through their jargon.

<?php

$a= array( '1' => 'elem 1', '2'=> 'elem 2', '3'=>' elem 3');
print_r($a);
echo ("<br></br>");
$b=serialize($a);
print_r($b);

?>

输出:

Array ( [1] => elem 1 [2] => elem 2 [3] => elem 3 ) 

a:3:{i:1;s:6:"elem 1";i:2;s:6:"elem 2";i:3;s:7:" elem 3";}

我无法理解第二个输出.除此之外,谁能举个例子说明我需要在使用之前序列化一个php数组的情况?

I cannot understand the second output. Besides that, can anyone give an example of a situation that I need to serialize a php array before using it?

推荐答案

PHP 数组或对象或其他复杂数据结构不能在运行的 PHP 之外传输或存储或以其他方式使用脚本.如果您想在一次脚本运行之外持久化这种复杂的数据结构,您需要序列化它.这只是意味着将结构放入一个较低的公分母"中,可以由 PHP 以外的东西处理,如数据库、文本文件、套接字.PHP 的标准函数serialize 只是表达这种东西的格式,它将数据结构序列化为PHP 独有的字符串表示,并且可以反转为PHP 对象使用反序列化.不过,还有许多其他格式,例如 JSON 或 XML.

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.

以这个常见问题为例:

PHP 和 Javascript 只能通过字符串进行通信.您可以非常轻松地将字符串 "foo" 传递给 Javascript.您可以非常轻松地将数字 1 传递给 Javascript.您可以轻松地将布尔值 truefalse 传递给 Javascript.但是如何将这个数组传递给 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 ) 

答案是序列化.对于 PHP/Javascript,JSON 实际上是更好的序列化格式:

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 可以轻松地将其反转为实际的 Javascript 数组.

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";}

但几乎只有 PHP 使用它,其他任何地方都很少支持这种格式.
不过,这很常见,也得到了很好的支持:

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.

这篇关于如何使用 php serialize() 和 unserialize()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆