PHP serialize and unserialize

The other day I finally decided to try using PHP’s serialize and unserialize functions; A function I’ve heard of, and read about, but never really used. As soon as I printed the output I realized two things:

  • Now I understand how those fields in IPB’s database work!
  • Fuck I could of saved so much time!

I am going to give an example or two of when you could use these functions. Say you have a web application, and you have a related links column in database. You store results as text seperated by newlines so you can easily output the result in the frontend, but in the backend you have to mess around with explodes and implodes to edit it. Instead, you store the related links in an array like this:

$array[0]['id']   = 124;
$array[0]['url']  = "/path/to/file";
$array[0]['type'] = "html";

Then you run serialize($array) and have yourself a string which should look like this:


This is the serialized version of an array which you can store in a database. Now, in your frontend and backend uses, you get the row, and use $array = unserialize($datarow1[‘related’]), and you have your normal array again. The possibilities for this function are endless, but it will save you time.

You should certainly head over to and read more about this function, although documentation is limited. The best way to see it’s power is to actually use it!  Try updating some of your older applications to use serialization, and see how much code and time you save.

One Reply to “PHP serialize and unserialize”

  1. There is a major problem with serialized class objects that should get stored in a database. The private members of a serialized class object are prefixed with . Some databases like PostgreSQL handles it as they should – as a control character – and are running into an error storing the data or stores incomplete data, so data gets lost and is not unserializeable.

Leave a Reply