PHP serialize serialized data and JSON formatted data analysis

Source: Internet
Author: User
This article covers PHP serialize serialized data and JSON formatted data analysis. For more information, see

This article covers PHP serialize serialized data and JSON formatted data analysis. For more information, see

PHP serialize serializes variables and returns a string expression with variable types and structures. JSON is a lighter and more friendly interface (AJAX, REST, etc) the format of data exchange. In fact, both of them reflect a Data Structure in the form of a string. So what is the difference between them?
Serialize serialization
In some old WEB systems, we may see a large string of text content stored in databases or text files that seem to have a special meaning. We will find that it has data type, structure, and other information, but it is not easy to read manually, it is only suitable for PHP programs to read. PHP serialize serializes and stores arrays. Let's assume there is an array like this:

$ Arr = array ("0" => array ("gameName" => "Deb", "homeName" => "Pretoria", "guestName" => "Brunswick ", "endTime" => "2015-08-21"), "1" => array ("gameName" => "Premier League", "homeName" => "Crystal Palace ", "guestName" => "Aston Villa", "endTime" => "2015-08-22 "));

We want to store the array content in a database or text file for reading elsewhere.

$ Serialize = serialize ($ arr); echo $ serialize;

We use PHP 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: "Pretoria"; s: 9: "guestName"; s: 12: "Brunswick"; s: 7: "endTime"; s: 10: "2015-08-21 ";} i: 1; a: 4: {s: 8: "gameName"; s: 6: "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 above output results seem complicated, but they are also very simple. It describes some data types and structures.
A: 2 It indicates that this is an array with two elements (array );
I: 0 indicates a sequence index;
A: 4 indicates four fields.
S: 8: "gameName" indicates that this is a string with 8 characters)
In actual development, we only store serialized data and do not care about the storage format and field meanings. If you want to restore serialized data to an array, use the unserialize () function.

Print_r (unserialize ($ serialize ));

The code above can print out an array.
JSON data parsing
We know that json_encode () and json_decode () functions can be used to operate JSON in PHP. Json_encode () can convert an array to text data in json format, which is easy to store and read, while json_decode () can directly convert json data into an array for convenient 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 JSON is used, the data space is less than serialize, and the Chinese string in the output result is encoded. It is a key-value correspondence, which is convenient for manual identification, in addition, the key is that data in JSON format is easy to read and recognize in other languages. Therefore, some people say it is a substitute for XML. JSON-format data can complete Asynchronous interaction with WEB Front-end JS. To restore json to an array, use the json_decode () function.
Print_r (json_decode ($ jsonencode, true ));
If you are interested in JSON applications, refer to this article: JSON applications in PHP.
Summary
PHP serialize makes it easy to store serialized arrays, while JSON-format data is not only easy to store but also can be read from other languages such as javascript. They may have minor differences in performance. If there are many frontend and backend interactions, JSON is recommended. Combined with PHP, Javascript, JSON, and Ajax, powerful data interaction functions can be completed.

There is so much detailed analysis on PHP serialize serialized data and JSON formatted data. If you want to know more, continue to pay attention to it.

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.