How to use php serialize() and unserialize() Function
In PHP, the complex data can not be transported or can not be stored. If you want to execute continuously a complex set of data beyond a single script then this serialize() and unserialize() functions are handy to deal with those complex data structures. The serialize() function is just given a compatible shape to a complex data structure that the PHP can handle that data after that you can reverse the work by using the unserialize() function.
Most often, we need to store a complex array in the database or in a file from PHP. Some of us might have surely searched for some built-in function to accomplish this task. Complex arrays are arrays with elements of more than one data-types or array. But, we already have a handy solution to handle this situation.
Serialize() Function: The serialize() is an inbuilt function PHP that is used to serialize the given array. The serialize() function accepts a single parameter which is the data we want to serialize and returns a serialized string.
- Syntax:
serialize( $values_in_form_of_array )
- Below program illustrate the Serialize() function.
Program:<?php// Complex array$myvar=array('hello',42,array(1,'two'),'apple');// Convert to a string$string= serialize($myvar);// Printing the serialized dataecho$string;?>chevron_rightfilter_none - Output:
a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i: 0;i:1;i:1;s:3:"two";}i:3;s:5:"apple";}
Unserialize() Function: The unserialize() is an inbuilt function php that is used to unserialize the given serialized array to get back to the original value of the complex array, $myvar.
- Syntax:
unserialize( $serialized_array )
- Below program illustrate both serialize() and unserialize() functions:
Program:<?php// Complex array$myvar=array('hello',42,array(1,'two'),'apple');// Serialize the above data$string= serialize($myvar);// Unserializing the data in $string$newvar= unserialize($string);// Printing the unserialized dataprint_r($newvar);?>chevron_rightfilter_none - Output:
Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple )
Note: For more depth knowledge you can check PHP | Serializing Data article.
Recommended Posts:
- PHP | SplObjectStorage unserialize() Function
- PHP | ArrayObject unserialize() Function
- PHP | ArrayObject serialize() Function
- PHP | SplObjectStorage serialize() Function
- How to serialize an object to query string using jQuery ?
- How to serialize an object into a list of URL query parameters using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.set.has() Function
- PHP Ds\Map sum() Function
- D3.js | d3.mean() function
- PHP | Ds\Map map() Function
- D3.js | d3.sum() function
- PHP | Ds\Map last() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



