PHP and MySQL (56 Blogs) Become a Certified Professional

Everything You Need To Know About Unserialize In PHP

Published on Sep 24,2019 7.8K Views

Everything You Need To Know About Unserialize In PHP

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,

So let us get started then,

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=&rdquo; black&rdquo;;
//serializes the object into a plain text string
$car1_serialized=serialize($car1);
//outputs the serialized text string
Echo &ldquo;This is the serialized data shown below:<br> &lsquo;$car1_serialized&rdquo;;
Echo&rdquo; <br><br>&rdquo;;
//unserializes the plain text string back into an object
$car1_unserialized=unserialize(($car1_serialized);
//outputs theobject and an object property
Echo &ldquo;This is the unserialized data shown below:<br>&rdquo;;
Echo var_dump($car1_unserialized);
Echo&rdquo; <br>;
Echo &ldquo;Object property:&rdquo;. $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 (&ldquo;red&rdquo;,&rdquo; pink&rdquo;,&rdquo; yellow&rdquo;,&rdquo; brown&rdquo;);
$colors_serialized=serialize($colors);
Echo &ldquo;This is serialized array shown below<br>:&rsquo;$colors_serialized&rdquo;;
$colors_unserialized= unserialized($colors_serialized);
Echo &ldquo;This is the unserializrd array shown below:<br>&rdquo;;
Echo&rdquo; <pre>&rdquo;;
Echo print_r($colors_unserialized);
Echo &ldquo;</pre>&rdquo;;
?>

 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.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Everything You Need To Know About Unserialize In PHP

edureka.co