Home >Backend Development >PHP Tutorial >Detailed graphic explanation of PHP serialization and deserialization functions

Detailed graphic explanation of PHP serialization and deserialization functions

墨辰丷
墨辰丷Original
2018-05-23 11:30:001864browse

This article mainly introduces PHP serialization and deserialization functions. Friends in need can refer to

Serialization and Deserialization

to simplify the complex Compress the data type into a string

serialize() Encode variables and their values ​​into text form
unserialize() Restore the original variables

1 .Create an $arr array to store basic user information and output the viewing results in the browser;

$arr=array();
$arr['name']='张三';
$arr['age']='22';
$arr['sex']='男';
$arr['phone']='123456789';
$arr['address']='上海市浦东新区';
var_dump($arr);

Output results:

array(5) { 
["name"]=> string(6) "张三" 
["age"]=> string(2) "22" 
["sex"]=> string(3) "男" 
["phone"]=> string(9) "123456789" 
["address"]=> string(21) "上海市浦东新区"
 }

2. Serialize the $arr array and assign it to the $info string, and output the viewing result in the browser;

$info=serialize($arr);
var_dump($info);

Output result:

string(140) "a:5:{s:4:"name";s:6:"张三";s:3:"age";s:2:"22";s:3:"sex";s:3:"男";s:5:"phone";s:9:"123456789";s:7:"address";s:21:"上海市浦东新区";}"

Use the serialize($arr) function to serialize the array The keys and values ​​of the elements in the string are concatenated into strings in regular order. The a:5 flag is serialized into an array containing 5 key-value pairs, and the s:4 flag content is a string containing 4 characters.

Through serialization, we can store some modular data in the form of strings in the database or session, etc., which can reduce the creation of many cumbersome data table fields. Of course, serialization as string storage will Adding extra space should be properly designed and applied.

3. Finally, use unserialize($info) to deserialize and restore the string to the array pattern we need;

$zhangsan=unserialize($info);
var_dump($zhangsan);

Output result:

array(5) {
["name"]=> string(6) "张三" 
["age"]=> string(2) "22" 
["sex"]=> string(3) "男" 
["phone"]=> string(9) "123456789" 
["address"]=> string(21) "上海市浦东新区" 
}

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHPCombined with jQuery to realize red and blue voting function special effects_jquery

PHPCombined with jQuery to implement comment like and dislike functions_jquery

When writing html5, debug and discover scripts php, etc. Webpage js, css, etc. are invalid_html5 tutorial skills

The above is the detailed content of Detailed graphic explanation of PHP serialization and deserialization functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn