How to Unserialize an Object or Array in PHP


In this article, we show how to unserialize an object or array in

By serializing data, an object or an array, we mean we convert the data to a plain text format. By unserializing the data, we convert it back to the native PHP code.

So if we serialize an object, we make it a plain text string. If we unserialize it, we get our back our previous object, as is, in PHP.

Why would an object need to be serialized?

An object is usually serialized when working with applications and languages outside of PHP. This is because other languages can't understand native PHP code, especially the complex data structures such as arrays and objects. Therefore, we serialize the data because it then becomes plain text format, which other languages can understand. So then we can use the serialized data and other languages can work directly with it, whether it's a MySQL database or Javascript. So we may serialize the data for it to work with other languages and applications outside of PHP.

However, when we're done with the outside applications, we may want to convert the serialized back into its original unserialized form (meaning back to the regular native PHP).

So, again, unserializing data is done through the unserialize function.

Below is an example of a code that serializes an object and then unserializes the object.

Unserializing an Object

So we'll now show how to unserialize an object in PHP.

This converts the object to a plain text string and then converts the plain text string back into an ojbect.

Below is PHP code that serializes and unserializes an object in PHP and outputs both the serialized data and the unserialized data.

The PHP output of this code is shown below.

Actual PHP Output

This is the serialized data shown below:

This is the unserialized data shown below:
object(cars)#2 (1) { ["color"]=> string(3) "red" }
Object property:red

So you can see the output above.

We'll explain the code of it now below.

So we create a class called cars. Inside we put a public property $color.

Right underneath this class we instantiate an object of the class, $car1. This is an object of the cars class.

We set the color property of this car equal to red.

We then serialize this $car1 object and store the serialized data into the variable $car1_serialized.

We output this serialized data, which is in plain text format in a string.

We then unserialized this serialized data thorugh the unserialize() functin.

We then output the breakdown of the object through the PHP var_dump() function.

We also output the color property of the $cars1_unserialized object so that you can see that it is in fact an object in that we're referencing as an object would be (showing it's an object). Of course, you don't have to call the variable $cars1_unserialized. You can call it the original value $car1 again.

Unserializing an Array

In the same way that we unserialized an object, we can unserialize an array.

An array is a complex structure that may need to be serialized to work with outside applications of PHP. Later on in the code, once we have dealt with the external languages or applications, we may need to convert it back into an array. And we do this through the unserialize function.

The PHP code below serializes an array and then unserializes the serialized data back into an array.

This PHP code yields the following shown below.

Actual PHP Output

This is the serialized array shown below
: 'a:4:{i:0;s:4:"blue";i:1;s:6:"orange";i:2;s:5:"black";i:3;s:5:"green";}'

This is the unserialized array shown below:

    [0] => blue
    [1] => orange
    [2] => black
    [3] => green

So it's really the same principle, just now with arrays.

We create an array, serialize it, show the serialized array, then unserialize it, getting back the original native PHP array. We then output the array through the print_r() function. We use pre tags just to make the array more readable.

And this is all that is required to unserialize data in PHP.

Related Resources

How to Serialize an Object or Array in PHP

HTML Comment Box is loading comments...