Data Serialization on PHP

If you are fairly new to PHP language, you may or may not be familiar with the idea of data serialization. Today in this post I will discuss how serialization works and how we can take advantage of this feature in PHP language. I will use examples to explain which I think may be the best way to learn stuff.

Serialization is a method of keeping data in simple text form. It can be used for both arrays and object. This method can be particularly very useful if you are building or planning to build any PHP and MySQL based application.

Serialize Data

Think of about your home address. You have street information, apartment or suite number, city, zip code, state and the country where you live in. We can easily organize these pieces of information within an array and then serialize them to store it on our MySQL database table row. This is how our address information should look like.

// address on an array
$address = array(
    'address1'   => '1000 Wall Street',
    'address2'   => 'Suite # 300',
    'city'       => 'New York',
    'state'      => 'NY',
    'zip'        => '1000',
    'country'    => 'United States');
// serialize the address
$sa = serialize($address);
// human readable format
print_r($sa);

From this simple snippet, you can see that I am using $address variable to list the address information with a key and their value. Now, in order to serialize my array, I am using a PHP function called serialize. So basically the $sa variable is holding our serialized address information. Up until this point, you won’t be able to see how serialization looks like. Let’s display the serialized version of our address, to do that I will be using the print_r() function. You should see the following line on your PHP page.

a:6:{s:8:"address1";s:16:"1000 Wall Street";s:8:"address2";s:11:"Suite # 300";s:4:"city";s:8:"New York";s:5:"state";s:2:"NY";s:3:"zip";s:4:"1000";s:7:"country";s:13:"United States";}

Now, this data can easily be stored on a single MySQL database table row. This method can particularly be more useful(depends on certain variables) if you are playing around with multidimensional array.

Unserialize Data

In order to display this data you must “unserialize” them first. Ofcourse, if you are storing this data on a database, you have to retrieve them first and then unserialize them. Let’s see how we can do that.

<?php
// unserialize the data
$ua = unserialize($sa);
// display on the screen
echo $ua['address1'];
?>

On this example I am using a variable ($ua) to hold the unserialized data (array) and then simply echo them out whenever required. If you want to see how the data has been unserialized, you can use the print_r() function just like before.

<?php
print_r($ua);
?>

It should display all the data (array keys and values). I hope you would find this post to be useful. Thanks.

References: serialize, unserialize, print_r

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.