A brief discussion on the use of PHP function serialize () and unserialize () _php Example

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:

$a =array ("Zhou", "full", "and", "Personal", "blog", "Www.jb51.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 this database
a:8:{i:0;s:3: "Zhou"; I:1;s:3: "Full"; I:2;s:3: "and"; I:3;s:6: "Individual"; I:4;s:6: "Blog"; i:5;s :: "Www.jb51.net"; I:6;s:9: "Zhou and"; I:7;s:12: "Personal blog";}
 
After the unserialize () deserialized data, the same as the previous $a array structure
([0] => Week [1] => full [2] => and [3] => individual [4] => Blog [5] =& Gt www.jb51.net [6] => Zhou and [7] => personal blog)

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

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

The output results are:

The data obtained after serialize serialization is also stored in the database
a:3:{s:4: "Name"; A:3:{i:0;s:3: "Zhou"; 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.jb51.net"; S:3: "All"; a:1:{s:21: "Zhou and personal blog"; s:20: "Www.jb51.net";}
 
The unserialize () deserialized data is the same as the previous $a array structure (
[name] => Array ([0] => Week [1] => full [2] => and) [Name_web URL] => Array ([0] => Zhou and [1] => www.jb51.net) [all] => array ([Zhou and personal blogs] => www.jb51.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.