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['id'] = 124; $array['url'] = "/path/to/file"; $array['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 php.net 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.