How to use Serialize () and unserialize () functions in PHP

Source: Internet
Author: User
Tags serialization

PHP function serialize ():

The function is to serialize the data and return a stored string that facilitates storing or passing PHP values without losing its type and structure. So we often see such a structure in the CMS database.

In general, we encapsulate data that is complex or data-free and not necessarily stored separately, into a multidimensional array that is serialize () to a string, and then put it into the database, when needed to be taken out to the group again to use, and took out the unserialize of the group is the PHP (), There's one more UN in front of you. Directly on Example:

The code is as follows Copy Code

$a =array ("Zhou", "full", "and", "Personal", "blog", "Www.111cn.net", "Zhou and", "personal blog");
$b =serialize ($a);
Print_r ($b);
echo "<br/>";
$c =unserialize ($b);
Print_r ($c);

The output results are:

The data obtained after serialize serialization is also stored in the database
A:8:{i:0;s:3: "Zhou"; I:1;s:3: "Full"; I:2;s:3: "and"; I:3;s:6: "Personal"; I:4;s:6: "blog"; i:5;s:20: "Www.111cn.net"; I:6;s:9: "Zhou and"; i:7; S:12: "Personal blog";}

The unserialize () deserialized data is the same as the previous $a array structure.
Array ([0] => Week [1] => full [2] => and [3] => individual [4] => Blog [5] => www.111cn.net [6] => Zhou and [7] => personal blog )

An example of a two-dimensional array on a complex point:

  code is as follows copy code

$a 1=array (
    ' name ' =>array ("weeks", "full", ' and '),
    ' Name_weburl ' =>array ("Zhou and", " Www.111cn.net "),
    ' all ' =>array (' Zhou and personal blog ' => ' www.111cn.net ')
);
$b 1=serialize ($ A1);
Print_r ($b 1);
echo "<br/>";
$c 1=unserialize ($b 1); 
Print_r ($c 1);

The

Output result is:


//The data obtained after serialize serialization is also stored in the database
A:3:{s:4: "Name"; A:3:{i:0;s:3: "Week"; I:1;s:3: "Full"; I:2;s:3: "and"; s:11: "Name_weburl"; A:2:{i:0;s:9: "Zhou and"; I:1;s:20: "Www.111cn.net"; S:3: "All"; a:1:{s:21: "Zhou and personal blog"; s:20: "Www.111cn.net";}
 
///Unserialize () deserialized data, as in previous $a array structure
Array ([name] => Array ([0] => Week [1] => full [2] => ; and) [Name_weburl] => Array ([0] => Zhou and [1] => www.111cn.net) [all] => array ([Zhou and personal blog] => www.111cn.net ) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.