Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
This article will introduce to unserialize in PHP, a function that lets you unserialize objects or arrays, with a detailed practical demonstration. Following pointers will be covered in this article,
Unserialize In PHP
The unserialize function converts from serialized data to actual data. By serializing data, an array or an object, we mean we convert the data to a plain text format. By unserializing the data, we convert it back to the PHP code. So if we serialize an object, we make it a plain text string. If we unserialize it, we get back our previous object, in PHP. When we’re done with the outside applications, we may want to convert the serialized back into its original unserialized form.
Now that we have some idea about what is Unserialize In PHP let us understand how to unserialize an object,
Unserializing an Object
This converts the object to a plain text string and then converts the plain text string back into an object. Below is PHP code that serializes and unserializes an object in PHP and outputs both the serialized data and the unserialized data.
<?php Class cars { Public $color; } $car1= new cars (); $car1->color=” black”; //serializes the object into a plain text string $car1_serialized=serialize($car1); //outputs the serialized text string Echo “This is the serialized data shown below:<br> ‘$car1_serialized”; Echo” <br><br>”; //unserializes the plain text string back into an object $car1_unserialized=unserialize(($car1_serialized); //outputs theobject and an object property Echo “This is the unserialized data shown below:<br>”; Echo var_dump($car1_unserialized); Echo” <br>; Echo “Object property:”. $car1_unserialized->color; ?>
Actual PHP Output
The serialized data shown below:
‘0:4:” cars”:1: {s:5:” color”; s:3:” black”;}’
The unserialized data shown below:
Object(cars)#2(1) {[“color”] =>string (3)” black”}
Object property: black
In the next bit of this article on ‘Unserialze in PHP’, we will learn how to array can unserialized,
Unserialize An Array
An array is a complex structure that may need to be serialized to work with outside applications of PHP. Once we dealt with the external applications or languages, we may need to convert it back into an array. And we do this through the unserialize function.
The PHP code serializes an array and then unserializes the serialized data back into an array.
<?php $colors=array (“red”,” pink”,” yellow”,” brown”); $colors_serialized=serialize($colors); Echo “This is serialized array shown below<br>:’$colors_serialized”; $colors_unserialized= unserialized($colors_serialized); Echo “This is the unserializrd array shown below:<br>”; Echo” <pre>”; Echo print_r($colors_unserialized); Echo “</pre>”; ?>
This PHP code yields the following shown below.
Actual PHP Output
The serialized array shown below:
‘a:4:{i:0;s:4:”red”;i:1;s:6:”pink”;i:2;s:5:”yellow”;i:3;s:5:”brown”;}’
The unserialized array shown below:
Array
(
[0] => red
[1] => pink
[2] => yellow
[3] => brown
)
}
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.
This brings us to the end of this article.
If you found this PHP article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of this article and I will get back to you.
edureka.co