On the use of PHP function serialize () and unserialize () _php instance

Source: Internet
Author: User
PHP function serialize ():

This 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 this structure in the CMS database.

In general, we will be complex or the amount of data is not necessary to separate the storage of data encapsulated into a multidimensional array through serialize () into a string, and then into the database, when needed to be taken out to the number of groups again, and took out the number of units used is PHP unserialize (), One more UN in front. Directly on the example:

$a =array ("Week", "full", "and", "Personal", "blog", "Www.php.net", "duet", "Personal blog"), $b =serialize ($a);p rint_r ($b); echo "
"; $c =unserialize ($b);p rint_r ($c);

The output is:

Data obtained after serialize serialization is also stored in the database a:8:{i:0;s:3: "Week"; 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.php.net "; I:6;s:9:" Duet and "; I:7;s:12:" Personal blog ";} The data obtained by Unserialize () deserialization is the same as the previous $ A array structure ([0] = = Week [1] = [2] = = AND [3] = [4] = + Blog [5] = WW w.php.net [6] = duet and [7]/= Personal blog)

In the previous example of a more complex two-dimensional array:

$a 1=array (  ' name ' =>array ("Week", "full", ' and '),  ' Name_weburl ' =>array ("Duet and", "www.php.net"),  ' all ' = Array (' Duet and personal blog ' = ' www.php.net ')), $b 1=serialize ($a 1);p Rint_r ($b 1); echo "
"; $c 1=unserialize ($b 1); Print_r ($c 1);

The output 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: "Duet and"; I:1;s:20: "Www.php.net";} S:3: "All"; a:1:{s:21: "Duet and personal Blog"; s:20: "Www.php.net";}} The data obtained by Unserialize () deserialization is the same as the previous $ A array structure ([name] = = Array ([0] = = Week [1] = full [2] = =) [Name_weburl] = > Array ([0] = duet and [1] = www.php.net) [All] = Array ([Duet and personal blog] = www.php.net))
  • Related Article

    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.