PHP 的 serialize() 函数是做什么用的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:23:41  来源:igfitidea点击:

What is PHP's serialize() function for?

phpserialization

提问by JasonDavis

I just learned about the serialize()andunserialize()functions. What are some uses for this? I know people serialize things to put into a database. Could you give me some example uses where it is helpful?

我刚刚了解了serialize()unserialize()功能。这有什么用途?我知道人们将东西序列化以放入数据库。你能给我一些有用的例子吗?

I also see serialized code in javascript, is this the same? Can a serialized string in javascript can be unserialized with php unserialize()?

我也在 javascript 中看到了序列化代码,这是一样的吗?javascript 中的序列化字符串可以用 php 反序列化unserialize()吗?

采纳答案by Tyler Carter

PHP serialize allows you to keep an array or object in a text form. When assigning arrays to things like $_SESSION, it allows PHP to store it in a text file, and then recreate it later. Serialize is used like this for objects and variables. (Just make sure you have declared the class the object uses beforehand)

PHP 序列化允许您以文本形式保存数组或对象。当将数组分配给 $_SESSION 之类的东西时,它允许 PHP 将它存储在一个文本文件中,然后再重新创建它。Serialize 像这样用于对象和变量。(只要确保您事先声明了对象使用的类)

Wordpress on the other hand uses it for a very similar method, by storing the serialized arrays directly in a database. If you keep a database of keys => values, this could be very beneficial because of the flexibility of arrays, you can store anything in the value parameter.

另一方面,Wordpress 将它用于非常相似的方法,将序列化的数组直接存储在数据库中。如果您保留键 => 值的数据库,由于数组的灵活性,这可能非常有益,您可以在 value 参数中存储任何内容。

And heres the link (courtesy of first commentor): http://us3.php.net/serialize

这是链接(由第一评论者提供):http: //us3.php.net/serialize

回答by Pascal MARTIN

I often see seralized data store in Database, and I really don't like this :

我经常在数据库中看到序列化的数据存储,我真的不喜欢这样:

  • it's really hard to work in SQL with that data : how do you write conditions on serialized data ? Even harder : how do you update it ? Do you really write a PHP script that fetches every lines, unserialize those, modifies them, re-serialize, and stores them back in DB ? :-(
  • the day you will want to migrate your data to another software, it'll require more work to migrate the data (and be even more work if the new software is not written in PHP, btw)
  • 使用这些数据在 SQL 中工作真的很难:你如何在序列化数据上编写条件?更难的是:你如何更新它?你真的写了一个 PHP 脚本来获取每一行,反序列化,修改它们,重新序列化,然后将它们存储回 DB 吗?:-(
  • 当您想要将数据迁移到另一个软件时,迁移数据将需要更多的工作(如果新软件不是用 PHP 编写的,则需要更多的工作,顺便说一句)

Still, I admit it is an easy way to store not-well-defined data... and I do sometimes use it for that...

尽管如此,我承认这是一种存储定义不明确的数据的简单方法......我有时会使用它......

Another use for serialization is to facilitate data exchange between two systems : sending objects through some kind of webservice, for instance, requires them to be serialized in some kind of way.

序列化的另一个用途是促进两个系统之间的数据交换:例如,通过某种 Web 服务发送对象,要求它们以某种方式进行序列化。

If the two systems are PHP, you could envisage using serialize/unserialize. But, here too, what if one of the system is not PHP anymore ? Using JSON or SOAP is probably a better choice : a bit harder at first, but probably a more long-term solution, as those formats are known in other languages too.

如果这两个系统都是 PHP,您可以设想使用serialize/ unserialize。但是,这里也是,如果系统之一不再是 PHP 呢?使用 JSON 或 SOAP 可能是更好的选择:一开始有点困难,但可能是一个更长期的解决方案,因为这些格式在其他语言中也是已知的。

One thing I use PHP's serializefunction is to store data in cache (like APC's user's cache), in a PHP application : you cannot store objects as-is : you have to serialize them. As the cache is used only by one application, it is not necessary to use a format known by many languages ; so, serialize is OK... And, to store data in cache, you should use a really fast serialization function -- and serialize is pretty fast ^^

我使用 PHP 的serialize功能的一件事是将数据存储在缓存中(如 APC 的用户缓存),在 PHP 应用程序中:您不能按原样存储对象:您必须序列化它们。由于缓存仅由一个应用程序使用,因此不必使用多种语言都知道的格式;所以,序列化没问题......而且,要将数据存储在缓存中,您应该使用非常快速的序列化功能——而且序列化非常快^^

回答by James Skidmore

I often use serializeto store important information in the database that isn't worth creating a whole new field for, but could be of use in the future.

我经常serialize用来将重要信息存储在数据库中,这些信息不值得为其创建一个全新的字段,但将来可能会有用。

For instance, if the user fills out a form that I only store a few elements from, but I want to keep them all in case I need them later, I'll serialize the array of form elements and store it in the database.

例如,如果用户填写了我只存储其中几个元素的表单,但我想保留所有元素以备以后需要它们,我将序列化表单元素数组并将其存储在数据库中。

回答by Andrey Prokhorov

Serialization is used when you want to pass data outside PHP, for example to Javascript.

当您想在 PHP 之外传递数据时使用序列化,例如传递给 Javascript。

Yeah, it often works to send strings, integers and strings as they are, but not more complex structures like arrays. Read more about passing a PHP array to Javascript here.

是的,它通常可以按原样发送字符串、整数和字符串,但不能发送更复杂的结构,如数组。在此处阅读有关将 PHP 数组传递给 Javascript 的更多信息。

You can also format your data as JSON that is widely supported in many languages, but then you loose PHP native types and your own classes when converting to it. Read more about JSON and PHP here.

您还可以将数据格式化为在许多语言中得到广泛支持的 JSON,但是在转换为它时会丢失 PHP 本机类型和您自己的类。在此处阅读有关 JSON 和 PHP 的更多信息。