serialize serialization
We may see the contents of a database or stored in a text file with a long list of seemingly has a special meaning in the text string some old WEB system. We look carefully will find that it has a data type and structure information, but it is not easy to read manual, it is only suitable PHP program reads. PHP's serialize the array is serialized and stored. We assume that there is such an array:
$arr = array(
"0" => array(
"gameName" => "a",
"homeName" => "b",
"guestName" => "c",
"endTime" => "2015-08-21"
),
"1" => array(
"gameName" => "d",
"homeName" => "e",
"guestName" => "f",
"endTime" => "2015-08-22"
)
);
We want the contents of the array is stored in a database or text file, so read elsewhere.
$serialize = serialize($arr);
echo $serialize;
We use PHP's serialize the array are serialized output the following results:
a:2:{i:0;a:4:{s:8:"gameName";s:6:"a";s:8:"homeName";s:15:"b";s:9:"guestName";s:12:"c";s:7:"endTime";s:10:"2015-08-21";}i:1;a:4:{s:8:"gameName";s:6:"d";s:8:"homeName";s:9:"e";s:9:"guestName";s:15:"f";s:7:"endTime";s:10:"2015-08-22";}}
The results of the above output looks more complicated, in fact, very simple, and it demonstrates is that some data types and structures.
a: 2 Description This is an array of two elements (array);
i: 0 refers to the sequence index;
a: 4 refers to four fields
s: 8: "gameName" have stated that this is a string of eight characters (string)
The actual development, we will only serialized data stored, the storage is not going to care about the format and meaning as well as fields. If the data you want to restore the serialized into an array, use unserialize () function can be.
print_r(unserialize($serialize));
The above code can print out the array.
JSON data analysis
We know, PHP operate JSON can use json_encode () and json_decode () two functions. json_encode () array can be converted into text data json format that is easy to store and read, and json_decode () can directly json data into an array, easy call.
$jsonencode = json_encode($arr);
echo $jsonencode;
Output:
[{"gameName":"\u5fb7\u4e59","homeName":"\u6bd4\u52d2\u8d39\u5c14\u5fb7","guestName":"\u4e0d\u4f26\u745e\u514b","endTime":"2015-08-21"},{"gameName":"\u82f1\u8d85","homeName":"\u6c34\u6676\u5bab","guestName":"\u963f\u65af\u987f\u7ef4\u62c9","endTime":"2015-08-22"}]
Obviously, the use of JSON, serialize the data space than less, the resulting output string encoded in Chinese, is a closer look at the key corresponding to the easy manual identification, but the key is the data in JSON format easy to read and identify other languages, so some people say it is XML alternatives. JSON-formatted data can be completed and WEB front-end JS asynchronous interaction. If you want to restore the json array, you can use json_decode () function.
print_r(json_decode($jsonencode,true));
summary
PHP's serialize the array is serialized for easy storage, and the data stored in JSON format is not only convenient, but also with other languages such as javascript read. They may be subtle differences in performance if more interactive use of front and rear ends, then I suggest using JSON, combined with PHP, javascript, JSON, and Ajax can complete the powerful data exchange capabilities.