PHP multiple serialization/deserialization methods

Source: Internet
Author: User
Serialization is the process of converting a variable to a saved or transmitted string. deserialization is to convert the string to the original variable when appropriate. These two processes can be combined to easily store and transmit data, making the program more maintainability

Serialization is the process of converting a variable to a saved or transmitted string. deserialization is to convert the string to the original variable when appropriate. These two processes can be combined to easily store and transmit data, making the program more maintainability.


1. serialize and unserialize functions


These two are common functions for serialization and deserialization of PHP Data.

$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');

// Serialize the array

$ S = serialize ($ );

Echo $ s;

// Output result: a: 3: {s: 1: "a"; s: 5: "Apple"; s: 1: "B"; s: 6: "banana"; s: 1: "c"; s: 7: "Coconut ";}

Echo'

';

// Deserialization

$ O = unserialize ($ s );

Print_r ($ o );

// Output result Array ([a] => Apple [B] => banana [c] => Coconut)

?>

When array values contain characters such as double quotes, single quotes, or colons, they may be deserialized. To overcome this problem, a clever technique is to use base64_encode and base64_decode.

$ Obj = array ();

// Serialization

$ S = base64_encode (serialize ($ obj ));

// Deserialization

$ Original = unserialize (base64_decode ($ s ));

However, base64 encoding increases the length of the string. To overcome this problem, it can be used with gzcompress.

// Define a function for serializing objects

Function my_serialize ($ obj)

{

Return base64_encode (gzcompress (serialize ($ obj )));

}

// Deserialization

Function my_unserialize ($ txt)

{

Return unserialize (gzuncompress (base64_decode ($ txt )));

}

2. json_encode and json_decode


JSON format serialization and deserialization are a good choice:


The output in json_encode and json_decode formats is much faster than that in serialize and unserialize formats.

The JSON format is readable.

The JSON format is smaller than the data returned by serialize.

JSON format is open and portable. You can also use it in other languages.

$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');

// Serialize the array

$ S = json_encode ($ );

Echo $ s;

// Output result: {"a": "Apple", "B": "banana", "c": "Coconut "}

Echo'

';

// Deserialization

$ O = json_decode ($ s );

In the preceding example, the json_encode output length is obviously shorter than the serialize output length in the previous example.


3. var_export and eval


The var_export function outputs the variable as a string. eval executes the variable as PHP code and deserializes it to obtain the content of the original variable.


$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');


// Serialize the array


$ S = var_export ($ a, true );


Echo $ s;


// Output result: array ('a' => 'apple', 'B' => 'bana', 'C' => 'Coconut ',)


Echo'

';


// Deserialization


Eval ('$ my_var ='. $ s .';');


Print_r ($ my_var );

4. wddx_serialize_value and wddx deserialize


The wddx_serialize_value function can serialize array variables and output them as XML strings.


$ A = array ('a' => 'apple', 'B' => 'bana', 'C' => 'coconut ');


// Serialize the array

$ S = wddx_serialize_value ($ );

Echo $ s;


// Output result (view the source code of the output string ):

Apple Banana Coconut


Echo'

';


// Deserialization

$ O = wddx_deserialize ($ s );

Print_r ($ o );

// Output result: Array ([a] => Apple [B] => banana 1 => Coconut)

It can be seen that the serialization of this format still occupies a lot of space due to the large number of XML tag characters.


Summary


All the above functions can be normally executed when serializing array variables, but the objects used are different. For example, json_encode fails to serialize the object. When deserializing an object, unserialize and eval have different effects.

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.