在 PHP 中序列化或 json ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2574728/
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
Serialize or json in PHP?
提问by datasn.io
So I need to encode an array in PHP and store it in plain text in MySQL database, my question is should I use serialize() or json_encode()? What are the advantages and disadvantages of each of them?
所以我需要在 PHP 中编码一个数组并将其以纯文本形式存储在 MySQL 数据库中,我的问题是我应该使用 serialize() 还是 json_encode()?它们各自的优缺点是什么?
I think either of them would do in this situation. But which one would you prefer and why? If it is for something other than an array?
我认为在这种情况下,他们中的任何一个都会这样做。但是你更喜欢哪一个,为什么?如果它用于数组以外的东西?
回答by Pascal MARTIN
Main advantage of serialize: it's specific to PHP, which means it can represent PHP types, including instances of your own classes -- and you'll get your objects back, still instances of your classes, when unserializing your data.
主要优点serialize:它是 PHP 特有的,这意味着它可以表示 PHP 类型,包括您自己的类的实例——并且在对数据进行反序列化时,您将返回您的对象,仍然是您的类的实例。
Main advantage of json_encode: JSON is not specific to PHP : there are libraries to read/write it in several languages -- which means it's better if you want something that can be manipulated with another languagethan PHP.
主要优点json_encode: JSON 不是 PHP 特有的:有一些库可以用多种语言读/写它——这意味着如果你想要一些可以用另一种语言而不是 PHP操作的东西,那就更好了。
A JSON string is also easier to read/write/modify by handthan a serialized one.
与序列化字符串相比,JSON 字符串也更易于手动读取/写入/修改。
On the other hand, as JSON is not specific to PHP, it's not aware of the stuff that's specific to PHP -- like data-types.
另一方面,由于 JSON 不是特定于 PHP 的,它不知道特定于 PHP 的东西——比如数据类型。
As a couple of sidenotes :
作为一些旁注:
- Even if there is a small difference in speed between those two, it shouldn't matter much : you will probably not serialize/unserialize a lot of data
- Are you sure this is the best way to store data in a database ?
- You won't be able to do much queries on serialized strins, in a DB : you will not be able to use your data in
whereclauses, nor update it without the intervention of PHP...
- You won't be able to do much queries on serialized strins, in a DB : you will not be able to use your data in
- 即使这两者之间的速度存在微小差异,也不应该有太大关系:您可能不会序列化/反序列化大量数据
- 您确定这是在数据库中存储数据的最佳方式吗?
- 您将无法在数据库中对序列化的字符串进行太多查询:您将无法在
where子句中使用您的数据,也无法在没有 PHP 干预的情况下更新它...
- 您将无法在数据库中对序列化的字符串进行太多查询:您将无法在
回答by Shozab Hasan
I did some analysis on Json Encoding vs Serialization in PHP. And I found that Json is best for plain and simple data like array.
我对 PHP 中的 Json Encoding vs Serialization 做了一些分析。我发现 Json 最适合像数组这样的简单数据。
See the results of my experiments at https://www.shozab.com/php-serialization-vs-json-encoding-for-an-array/
在https://www.shozab.com/php-serialization-vs-json-encoding-for-an-array/查看我的实验结果
回答by Max
Another advantage of json_encodeover serializeis the size. I noticed that as I was trying to figure out why our memcacheused memory was getting so big, and was trying to find ways to reduce is:
json_encodeover 的另一个优点serialize是尺寸。我注意到,当我试图弄清楚为什么我们memcache使用的内存变得如此之大时,并试图找到减少的方法是:
<?php
$myarray = array();
$myarray["a"]="b";
$serialize=serialize($myarray);
$json=json_encode($myarray);
$serialize_size=strlen($serialize);
$json_size=strlen($json);
var_dump($serialize);
var_dump($json);
echo "Size of serialized array: $serialize_size\n";
echo "Size of json encoded array: $json_size\n";
echo "Serialize is " . round(($serialize_size-$json_size)/$serialize_size*100) . "% bigger\n";
Which gives you:
这给了你:
string(22) "a:1:{s:1:"a";s:1:"b";}"
string(9) "{"a":"b"}"
Size of serialized array: 22
Size of json encoded array: 9
Serialize is 59% bigger
Obviously I've taken the most extreme example, as the shorter the array, the more important the overhead with serialize (relative to the initial object size, due to formatting which imposes a minimum number of characters no matter how small the content). Still from a production website I see serialized array that are 20% bigger than their json equivalent.
显然我已经举了一个最极端的例子,因为数组越短,序列化的开销就越重要(相对于初始对象大小,因为无论内容多小,格式都会强加最少的字符数)。仍然在生产网站上,我看到序列化数组比它们的 json 等价物大 20%。
回答by cletus
Well firstly serializing an array or object and storing it in a database is typically a code smell. Sometimes people end up putting a comma separated list into a column and then get into all sorts of trouble when they later find out they need to query on it.
首先序列化一个数组或对象并将其存储在数据库中通常是一种代码异味。有时,人们最终会将逗号分隔的列表放入一列中,然后当他们发现需要对其进行查询时遇到各种麻烦。
So think very carefully about that if this is that kind of situation.
因此,如果是这种情况,请仔细考虑。
As for the differences. PHP serialize is probably more compact but only usable with PHP. JSON is cross-platform and possibly slower to encode and decode (although I doubt meaningfully so).
至于差异。PHP 序列化可能更紧凑,但仅适用于 PHP。JSON 是跨平台的,编码和解码可能较慢(尽管我对此表示怀疑)。
回答by Vidda
First, thanks to Shozab Hasan and user359650 for these tests. I was wondering which choice was the best and now i know:
首先,感谢 Shozab Hasan 和 user359650 进行这些测试。我想知道哪个选择是最好的,现在我知道了:
To encode a simple array, JSON which is OK with both PHP AND javascript, maybe other languages.
要编码一个简单的数组,JSON,这对 PHP 和 javascript 都可以,也许其他语言。
To encode a PHP object, serialize is a better choice because of specificity of PHP Objects only instanciable with PHP.
要编码 PHP 对象,序列化是更好的选择,因为 PHP 对象的特殊性只能与 PHP 实例化。
To store datas, either store encoded datas in a file or use MySQL with standard format. It would be much easier to get your datas back. MySQL has great functions to get datas the way you'd like to get them without PHP treatment.
要存储数据,请将编码数据存储在文件中或使用标准格式的 MySQL。取回数据会容易得多。MySQL 具有强大的功能,可以按照您希望的方式获取数据,无需 PHP 处理。
I've never made any test but i think that file storage is the best way to store your datas if system file sorting is enough to get back your files in alphabetical/numeral order. MySQL is to greedy for this kind of treatment and uses file system too...
我从未做过任何测试,但我认为如果系统文件排序足以按字母/数字顺序取回文件,那么文件存储是存储数据的最佳方式。MySQL对这种处理很贪婪,也使用文件系统......
回答by selfawaresoup
If you data will never has to leave your PHP application, I recommend serialize() because it offers a lot of extra functionality like __sleep() and __wakeup() methods for your objects. It also restores objects as instances of the correct classes.
如果你的数据永远不会离开你的 PHP 应用程序,我推荐 serialize() 因为它为你的对象提供了很多额外的功能,比如 __sleep() 和 __wakeup() 方法。它还将对象恢复为正确类的实例。
If you will pass the serialized data to another application, you should use JSON or XML for compatibility.
如果您要将序列化数据传递给另一个应用程序,您应该使用 JSON 或 XML 以实现兼容性。
But storing a serialized objet into a database? Maybe you should think about that again. It can be real trouble later.
但是将序列化的对象存储到数据库中?也许你应该再考虑一下。以后可能会很麻烦。

