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:
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.
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;
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.