Php Serialization functions serialize () and unserialize () are compared with native functions. Comparison of php Serialization functions serialize () and unserialize () with native functions this article mainly introduces the comparison of php Serialization functions serialize () and unserialize () with php native serialization methods, comparison between php Serialization functions serialize () and unserialize () and native functions
This article mainly introduces the comparison between php Serialization functions serialize () and unserialize () and php native serialization methods. For more information, see.
Php has a good method of formatting strings and converting them into arrays or objects, that is, serialization.
There are two methods to serialize variables.
The following example uses the serialize () and unserialize () functions:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// A complex array $ Myvar = array ( 'Hello ', 42, Array (1, 'two '), 'Apple' ); // Convert to a string $ String = serialize ($ myvar ); Echo $ string; /* Prints A: 4: {I: 0; s: 5: "hello"; I: 1; I: 42; I: 2; a: 2: {I: 0; I: 1; I: 1; s: 3: "two";} I: 3; s: 5: "apple ";} */ // You can reproduce the original variable $ Newvar = unserialize ($ string ); Print_r ($ newvar ); /* Prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */ |
This is the native PHP Serialization method.
However, as JSON has gained popularity in recent years, PHP5.2 has added support for JSON format.
Now you can use the json_encode () and json_decode () functions:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
// A complex array $ Myvar = array ( 'Hello ', 42, Array (1, 'two '), 'Apple' ); // Convert to a string $ String = json_encode ($ myvar ); Echo $ string; /* Prints ["Hello", 42, [1, "two"], "apple"] */ // You can reproduce the original variable $ Newvar = json_decode ($ string ); Print_r ($ newvar ); /* Prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */ |
This will be more effective, especially compatible with many other languages such as JavaScript.
Note: For complex objects, some information may be lost.
The above is all the content of this article. I hope you will like it.
Note <>: For more exciting tutorials, please pay attention to the help house programming
Comparison between serialize () and unserialize () and native functions this article mainly introduces the comparison between php Serialization functions serialize () and unserialize () and php native serialization methods...