52

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?

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?

datasn.io
  • 12,564
  • 28
  • 113
  • 154
  • 3
    What kind of array you want to store? Most newbies take relation databases wrong and misuse it. It's always better to make a table for such an array – Your Common Sense Apr 04 '10 at 14:41
  • 1
    Arrays with variable / volatile indexes and with a variable number of items. It'd be almost impossible to create a table structure for the data. – datasn.io Apr 04 '10 at 23:02
  • 1
    Duplicate of http://stackoverflow.com/questions/804045/preferred-method-to-store-php-arrays-json-encode-vs-serialize – David d C e Freitas May 04 '11 at 23:15
  • Beware of associative arrays vs stdClass objects using JSON .. http://jondavidjohn.com/blog/2012/09/problem-using-json-to-serialize-php-structures – jondavidjohn Oct 24 '12 at 17:50

6 Answers6

55

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.


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 language than PHP.

A JSON string is also easier to read/write/modify by hand than a serialized one.

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.


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 where clauses, nor update it without the intervention of PHP...
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 3
    since PHP 5.4.0 you can now customize JSON-serialization by implementing the [JsonSerializable](http://php.net/manual/en/class.jsonserializable.php) interface. – mindplay.dk May 04 '12 at 02:35
  • 1
    On your last point - there's no reason you can't use the contents of serialized/json'd strings in `WHERE` clauses. The whole idea is that these two methods boil down complex types like arrays and objects to string representation. I agree that there are better ways to do this if you're considering using a database. I'd also like to add a bump to the `json_encode`'d string: it's a lot more robust as it doesn't rely on lengths for it's values. – phatskat Nov 20 '13 at 22:14
  • To date, almost all the major relational DBMSs (SQL Server, PostgreSQL, MySQL) support JSON columns/values natively and make it easy to navigate the object contained in a JSON string. It is even possible to create indexes on the fields of JSON objects stored in a column (in SQL Server, it is necessary to create a virtual indexed column). Storing JSON data in a relational DB is ok; indeed, it is particularly useful for storing aggregates such as domain events. – nickshoe Apr 19 '23 at 07:25
13

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.

See the results of my experiments at https://www.shozab.com/php-serialization-vs-json-encoding-for-an-array/

Shozab Hasan
  • 567
  • 5
  • 11
  • 1
    great overview of the performance of json_(encode/decode) vs. serialize()/unserialize(). I'd love to see some other perspectives added to your blog (or here), as I'm not a performance expert. – Bob Gregor Sep 18 '13 at 21:33
8

Another advantage of json_encode over serialize is the size. I noticed that as I was trying to figure out why our memcache used memory was getting so big, and was trying to find ways to reduce is:

<?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.

Max
  • 12,794
  • 30
  • 90
  • 142
5

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).

cletus
  • 616,129
  • 168
  • 910
  • 942
  • There can be valid use cases for this. A database queue for example. The blob is never queried but simply deserialized when it comes to processing that record in the queue. – Peter Kelly Jan 21 '13 at 10:56
  • WordPress is a common example of PHP and the nature of the posts table pushes the user towards that kind of structure. So in that case it works with the coding environment, even though it's obviously not relational. – strattonn Jun 18 '15 at 04:34
3

First, thanks to Shozab Hasan and user359650 for these tests. I was wondering which choice was the best and now i know:

To encode a simple array, JSON which is OK with both PHP AND javascript, maybe other languages.

To encode a PHP object, serialize is a better choice because of specificity of PHP Objects only instanciable with 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.

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...

Vidda
  • 31
  • 1
3

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.

If you will pass the serialized data to another application, you should use JSON or XML for compatibility.

But storing a serialized objet into a database? Maybe you should think about that again. It can be real trouble later.

selfawaresoup
  • 15,473
  • 7
  • 36
  • 47