PHP's serialize serialization data and JSON formatted data analysis _php techniques

Source: Internet
Author: User
Tags serialization

PHP's serialize is to serialize the variable, returning a string expression with variable type and structure, while JSON is a lighter and friendlier format for interface (AJAX, rest, and so on) data interchange. In fact, both are in a string of ways to reflect a data structure. So what's the difference between them?
Serialize serialization
we may see in some old web systems that there is a large string of text content in a database or in a text file that seems to have special meaning. We look closely to see that it has data types and structure information, but it is not easy to manually read, it is only suitable for PHP program reading. The Serialize of PHP serializes and stores the array. We assume that there is an array:

$arr = Array ( 
  "0" => Array (" 
    gamename" => "de B", 
    "Homename" => "Bielefeld", 
    "Guestname" => "Braunschweig" , " 
    endtime" => "2015-08-21" 
  ), 
  "1" => Array ( 
    "Gamename" => "Premier League", 
    "Homename" => "Crystal Palace", 
    "Guestname" => "Aston Villa", 
    "Endtime" => "2015-08-22" 
  ) 
; 

We're going to store this array content in a database or text file so that we can read it 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: "De B"; S:8: "Homename"; s:15: "Bielefeld"; S:9: "Guestname"; s:12: "Braunschweig"; s:7: "Endtime"; s : 10: "2015-08-21";} I:1;a:4:{s:8: "Gamename"; s:6: "The Premier League"; S:8: "Homename"; S:9: "Crystal Palace"; S:9: "Guestname"; s:15: "Aston Villa"; s:7: "Endtime"; s:10: " 2015-08-22 ";}} 

The results of the above output look more complex, but also very simple, it shows some data types and structure.
A:2 indicates that this is an array of two elements (array);
I:0 refers to the sequence index;
A:4 refers to 4 fields
S:8: "Gamename" indicates that this is a 8-character string (string)
In actual development, we only store the serialized data and do not care about the storage format and the field meaning. If you want to restore the serialized data to an array, use the Unserialize () function.

Print_r (Unserialize ($serialize)); 

The above code can print several groups.
JSON Data parsing
we know that PHP operations JSON can use the Json_encode () and Json_decode () two functions. Json_encode () can convert an array into JSON-formatted text data for easy storage and reading, while Json_decode () can convert JSON data directly into arrays for easy invocation.

$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 serialize, the output of the results of the Chinese character string is encoded, look closely is the key value, easy to identify, and the key is JSON format data to facilitate other languages to read and identify, so some people say it is an XML substitute. JSON-formatted data can be done asynchronously with the Web front-end JS. If you want to restore the JSON to an array, you can use the Json_decode () function.
Print_r (Json_decode ($jsonencode, true));
About the application of JSON interested students can refer to this site article: PHP in the application of JSON
Summary
PHP's serialize is easy to store after serializing arrays, while JSON-formatted data is not only easy to store but also readable with other languages such as JavaScript. They may be slightly different in performance, and it is recommended that you use JSON, combined with PHP, Javascript, JSON, and Ajax, to perform powerful data interactions if there is a lot of interaction between the front and back ends.

For more information on PHP's serialize serialization data and JSON formatted data, keep your eye on it if you want to learn more.

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.