PHP’s serialize serializes variables and returns a string expression with variable type and structure , while JSON is a lighter and more friendly format for data exchange through interfaces (AJAX, REST, etc.). In fact, both of them embody a data structure in the form of a string. So what's the difference between them?
serialize
In some old WEB systems, we may see a large string of string text content that seems to have special meanings stored in the database or in the text file. If we look closely, we will find that it has information such as data type and structure, but it is not easy to read manually. It is only suitable for PHP programs to read. PHP's serialize serializes and stores arrays. Let's assume there is an array like this:
$arr = array( "0" => array( "gameName" => "德乙", "homeName" => "比勒费尔德", "guestName" => "不伦瑞克", "endTime" => "2015-08-21" ), "1" => array( "gameName" => "英超", "homeName" => "水晶宫", "guestName" => "阿斯顿维拉", "endTime" => "2015-08-22" ) );
We want to store the contents of this array in a database or text file so that it can be read elsewhere.
$serialize = serialize($arr); echo $serialize;
We use PHP's serialize to serialize the array and output the following results:
a:2:{i:0;a:4:{s:8:"gameName";s:6:"德乙";s:8:"homeName";s:15:"比勒费尔德";s:9:"guestName";s:12:"不伦瑞克";s:7:"endTime";s:10:"2015-08-21";}i:1;a:4:{s:8:"gameName";s:6:"英超";s:8:"homeName";s:9:"水晶宫";s:9:"guestName";s:15:"阿斯顿维拉";s:7:"endTime";s:10:"2015-08-22";}}
The above output result looks complicated, but it is actually very simple. It describes some data types and structures.
a:2 indicates that this is an array with two elements;
i:0 refers to the sequence index;
a:4 means there are 4 fields
s:8:"gameName" indicates that this is an 8-character string
In actual development, we only store the serialized data and do not care about the storage format and field meaning. If you want to restore the serialized data into an array, use the unserialize() function.
print_r(unserialize($serialize));
The above code can print out an array.
JSON data parsing
We know that PHP can use the json_encode() and json_decode() functions to operate JSON. json_encode() can convert an array into text data in json format, which is convenient for storage and reading, while json_decode() can directly convert json data into an array for easy calling.
$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, after using JSON, the data space is less than that of serialize. The Chinese strings in the output results are encoded. If you look closely, they correspond to key values, which is convenient for manual identification. The key is that the data in JSON format is easy to read in other languages. And recognized, so some people say it is an XML replacement. Data in JSON format can complete an asynchronous interaction process with WEB front-end JS. If you want to restore json to an array, you can use the json_decode() function.
print_r(json_decode($jsonencode,true));
Students who are interested in the application of JSON can refer to the article on this site: Application of JSON in PHP
Summary
PHP's serialize serializes arrays for easy storage, and data in JSON format is not only easy to store but can also be read with other languages such as javascript. They may have subtle differences in performance. If there is a lot of front-end and back-end interaction, it is recommended to use JSON. Combining PHP, Javascript, JSON and Ajax can complete powerful data interaction functions.
That’s it for the detailed analysis of PHP’s serialize data and JSON formatted data. If you want to know more, please continue to pay attention.