Original link: http://hi.baidu.com/lostdays/blog/item/8d76c300ec4e3c15738b65fa.html
General Catalogue
What, why, how
What
Why
How
PHP serialization Syntax
PHP Serialization Instance
Serialized into json-in JavaScript using json2.js
Serialized into json-in JavaScript using prototype.js
PHP and JSON
Json_decode function
Json_encode function
Json_decode function Instance
Json_encode function Instance
Practice is the truth
Background notes
Front desk JavaScript section
Background PHP section
What, why, how
What
Ok, dear friends, let us start this new concept of the journey, serialization of this topic may have not been more attention, things actually originated from that day I casually turn over PHP manual, found this serialization function, then idle to boring and do a wordpress plugin, This time by the way the serialization, found in some occasions is very convenient.
Let's start by explaining the serialization: In short, serialization is the process of converting variables into byte streams. Serialization of the proposed, effectively solve the problem of the preservation and transmission of objects, for example, I built an object in JavaScript, I now want to save this object to the server-side database, then how I do it, this time is often used to the serialization of objects. Json,json (JavaScript Object Notation) is a lightweight data interchange format that has to be mentioned in JavaScript serialization. Easy to read and write, but also easy to machine parse and generate. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.
People usually compare JSON with XML, both of which are flattened objects (later we explain the "flattening"), XML is characterized by a rigorous structure, and JSON is characterized by easy-to-read, easy-to-use program analysis, Because it is very simple to convert an object into a character stream, such as the following code:
Code:
{"Type": "Human", "Name": "Hanguofeng", "Age": 22}
is a JSON expression, he saves an object, how can we restore it to an object? Very simple, as follows:
Code:
var animal_str = {"Type": "Human", "Name": "Hanguofeng", "Age": 22};
var animal2=eval ((+ animal_str +));
We use the JavaScript evaluation function to perform the operation of the JSON expression and return the value to get an object, here, I think you will be like me, the creator of the JSON format to admire the thinking of it. Originally said serialization, "careless" to talk about JSON, and talk so much, hehe, irrelevant? No, PHP serialization and JSON are very similar, a PHP serialization expression is as follows:
Code:
A:3:{s:4: "Type"; S:5: "Human"; s:4: "Name"; s:10: "Hanguofeng"; s:3: "Age"; S:2: "20";}
He seems to have a somewhat similar structure to JSON, in fact, this expression is the serialization result of the following array:
Code:
$animal =
Array
(
"Type" = "human",
"Name" = "Hanguofeng",
"Age" = "20"
);
OK, some of the introductions above just let you see what serialization and JSON are, and you don't have to be overly tangled in the code here, and we'll talk about it in detail later, so let's discuss why we're using serialization.
Why
Serialization is first and foremost as a convenience for data transmission, as I started my question in JavaScript, I have created an object on the Web, and I want to save this object to the server-side database and what I should do, which is actually a "how do I submit an object from the browser to the server" The problem, in this transmission process, we know, actually only can pass the character stream, the character stream is one dimension (flat), but many objects are multidimensional, if the object to be passed is a string, then it is very simple, we directly use it as the content to pass it, If the object to be passed is an array or other structure, we need to use a character stream to describe him, like in the telephone, I ask your name is what, you will tell me, your name is Zhang San, John Doe, and I ask you, you look like, you need to describe to me in words, The medium of our data transmission is often the same as this telephone line, can only pass the character stream, and we describe the object of the process, is actually the process of serialization.
In addition, serialization can also be used for persistent storage of objects, perhaps you have been like me, think of a field in the database to store an object, now we can do this very simple, and your database field does not need to be set to a special format, set to varchar (of course, If the object is large, you may need to set it to text).
How
PHP serialization Syntax
Well, I think you know all about what and why, and in the end of this section we're going to point to something more theoretical, that is, how to serialize and crossdress the data using PHP, how to serialize the JavaScript object (that is, into JSON format) and how to crossdress it. The last is how to build a relationship between JSON and PHP serialization.
PHP provides us with two functions for serialization and crossdress operations, the two functions are: Serialize () and unserialize (), they apply to PHP4 and PHP5, respectively:
Serialize ()
(PHP 4, PHP 5, PECL axis2:0.1.0-0.1.1)
serialize-to obtain a stored representation value
Description
String serialize (mixed $value)
Get a description value that can be stored
This function is used for lossless storage or for passing PHP variable values and structures.
If you need to return the serialized value back to the PHP variable, you can use the Unserialize () function.
Parameters
Value
An expression that is serialized. Serialize () handles all types except the resource pointer, and you can even serialize the array that contains the elements that point to itself. You serialize the array containing the circular pointer or the object will be stored, the other points will be lost.
When serializing an object, PHP attempts to call its member function __sleep () first. This allows objects such as final cleanup work to be done before serialization. Similarly, when you use the Unserialize () function to restore an object, the member function __wakeup () is called.
return value
Returns a string of byte-stream expressions that can be stored in the containing object at any location.
Unserialize ()
(PHP 4, PHP 5, PECL axis2:0.1.0-0.1.1)
unserialize-get a PHP variable value from a stored expression
Description
Mixed Unserialize (String $str)
Unserialize () Gets a serialization variable of a simple type and converts it back to the value of the PHP variable.
Parameters
Str
Serialized string
If the variable being crossdress is an object, then PHP will automatically attempt to execute the object's __wakeup () member function (if it exists) after the object's structure has been successfully restored.
UNSERIALIZE_CALLBACK_FUNC directive: You can set the callback function to execute in this procedure, if an undefined class should be instantiated at crossdress (to avoid getting an incomplete object "__php_incomplete_class"). You can use Php.ini,ini_set () or. htaccess to define "Unserialize_callback_func". When a class that is not defined is instantiated, it is called. Masking this feature simply sets it to empty.
return value
Returns the converted value, possibly a Boolean, real, floating-point, String, array, or object.
If the incoming string cannot be crossdress, it returns false and throws a notice error.
(translated from PHP manual)
PHP Serialization Instance
Serialization and crossdress serialization of arrays
OK, let's use the example to learn, first of all, please create a sample1.php file, we use the following statement in this file for creating a hash array:
Code:
$animal =
Array
(
"Type" = "human",
"Name" = "Hanguofeng",
"Age" = "20"
);
?>
To test the value of this array, you can use the Print_r () function to output the array, and the result is as follows:
Code:
Array
(
[Type] = Human
[Name] = Hanguofeng
[Age] = 20
)
Then we'll serialize him. The serialized code is as follows:
Code:
$animal =
Array
(
"Type" = "human",
"Name" = "Hanguofeng",
"Age" = "20"
);
$animal _ser=serialize ($animal);
Echo ($animal _ser);
?>
Here we serialize the array $animal, save the returned serialized string in the variable $animal_ser, and output the result:
Code:
A:3:{s:4: "Type"; S:5: "Human"; s:4: "Name"; s:10: "Hanguofeng"; s:3: "Age"; S:2: "20";}
Let's take a simple parse of this string:
A:3 indicates that this is an array-type Object (a), he has three built-in objects (3)
The part of the curly brace is a comma-separated list of object expressions, with s:4: "Type" as an example, he represents a string (s), a length of 4 bits (4), and a value of "type", which is the key of the first element of the hash array.
The latter part and so on, let's not go into this, you can try to serialize the various objects to see how the serialized string is constructed.
Let's look at the crossdress of the array and restore the serialized string we generated above to an array, with the following code:
Code:
$animal _ser=a:3:{s:4: "type"; S:5: "Human"; s:4: "Name"; s:10: "Hanguofeng"; s:3: "Age"; S:2: "20";}; <
http://www.bkjia.com/PHPjc/486569.html www.bkjia.com true http://www.bkjia.com/PHPjc/486569.html techarticle original link: http://hi.baidu.com/lostdays/blog/item/8d76c300ec4e3c15738b65fa.html Total directory What, why, how, why? How PHP serialization syntax PHP serialization instances in JavaScript string ...