In php, The serialize () and unserialize () functions are a pair of functions. This article will introduce the use cases of the serialize () and unserialize () functions for you, hope to help you.
In php, The serialize () and unserialize () functions are a pair of functions. This article will introduce the use cases of the serialize () and unserialize () functions for you, hope to help you.
Php function serialize ():
This function is used to serialize data and return a stored string. This function stores or transmits PHP values without losing its type and structure. So we often see this structure in the cms database.
Generally, we encapsulate data that is complex or has a large amount of data but does not need to be stored separately into a multi-dimensional array and convert it into a string through serialize (), and then store it in the database. If necessary, we can convert it into an array and use it again, the php unserialize () is used to convert the result into an array. There is an additional un. Example:
$ A = array ("Week", "full", "and", "individual", "blog", "www.jb51.net", "week full and ", "personal blog"); $ B = serialize ($ a); print_r ($ B); echo"
"; $ C = unserialize ($ B); print_r ($ c );
The output result is:
// The data obtained after serialize serialization is 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: "individual"; I: 4; s: 6: "blog "; i: 5; s: 20: "www.jb51.net"; I: 6; s: 9: "weekly full and"; I: 7; s: 12: "personal blog ";} // The data obtained after unserialize () deserialization, like the previous $ a Array structure, Array ([0] => week [1] => full [2] => and [3] => individual [4] => blog [5] => [6] => weekly full and [7] => personal blog)
The preceding example of a complex two-dimensional array:
$ A1 = array ('name' => array ("Week", "full", "and"), 'name _ weburl' => array ("week full and ", "www.jb51.net"), 'all' => array ('Weekly full and personal blog '=> 'www .jb51.net'); $ b1 = serialize ($ a1 ); print_r ($ b1); echo"
"; $ C1 = unserialize ($ b1); print_r ($ c1 );
The output result is:
// The data obtained after serialize serialization is 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: "full week and"; I: 1; s: 20: "www.jb51.net";} s: 3: "all "; a: 1: {s: 21: "weekly full and personal blog"; s: 20: "www.jb51.net" ;}// the data obtained after unserialize () deserialization, like the previous $ a Array structure, Array ([name] => Array ([0] => week [1] => full [2] => and) [name_weburl] => Array ([0] => weekly full and [1] =>) [all] => Array ([weekly full and personal blog] => ))