PHP serialization functions serialize () and unserialize () vs. native functions
This article mainly introduces PHP serialization functions serialize () and unserialize () and PHP native serialization method comparison, the need for small partners can refer to.
PHP has a good way to format strings and convert an array or object, that is, serialization processing.
There are two ways 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 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 |
A complex array $myvar = Array ( ' Hello ', 42, Array (1, ' both '), ' 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: "Both";} 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] = ) [3] = Apple ) */ |
This is the native PHP serialization method.
However, thanks to JSON's 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 13 /p> + + / / / + + + + 25 + + { +/ + + / + |
A complex array $myvar = Array ( ' Hello ', 42, Array (1, ' both '), ' Apple ' ); Convert to a string $string = Json_encode ($myvar); Echo $string; /* Prints ["Hello", 42,[1, "I"], "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] = ) [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 mentioned is the whole content of this article, I hope you can like.
Note < > : More Exciting tutorials please focus on helping the home program
http://www.bkjia.com/PHPjc/996760.html www.bkjia.com true http://www.bkjia.com/PHPjc/996760.html techarticle PHP serialization functions serialize () and unserialize () vs. native functions This article mainly introduces the PHP serialization function serialize () and unserialize () compared to the native serialization method of PHP, there are ...