PHP serialize serialized data and JSON formatted data analysis, serializejson_php tutorial

Source: Internet
Author: User

PHP serialize serialized data and JSON formatted data analysis, Serializejson


PHP's serialize is to serialize variables, return a string expression with variable type and structure, and JSON is a lighter and friendlier format for interface (AJAX, rest, etc.) data exchange. In fact, both are in a string of the way to embody a data structure. So what's the difference between them?
Serialization of Serialize
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 a special meaning. We look closely and find that it has data type and structure information, but it is not easy to read manually, it is only suitable for PHP program reading. The PHP serialize serializes and stores the array. Let's say we have an array like this:

$arr = Array ("   0" = = Array (     "gamename" = "de-B",     "homename" = "Bielefeld",     "Guestname" and "Braunschweig" , "     endTime" and "2015-08-21"   ),   "1" = = Array (     "gamename" = "Premier League",     "Homename" and "Crystal Palace", c10/> "Guestname" = "Aston Villa",     "EndTime" and "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.

We use PHP's serialize to serialize the array, outputting the following results:

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

The above code can print the array.
JSON data parsing
We know that PHP operation JSON can use Json_encode () and Json_decode () two functions. Json_encode () can convert an array into JSON-formatted text data, which is convenient for storing and reading, while Json_decode () can convert JSON data directly into arrays for easy invocation.

Output:

Obviously, after using JSON, the data space is less than serialize, the output of the result of the Chinese character string is encoded, look closely is the key value corresponding, easy to identify, and the key is the JSON format of the data to facilitate reading and recognition in other languages, 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 Application of JSON
Summarize
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 to use JSON if the front-end interaction is used more, with PHP, Javascript, JSON, and Ajax to accomplish powerful data interaction capabilities.

About PHP serialize serialized data and JSON formatted data detailed analysis is so much, if you want to learn more about the content, please continue to follow.

http://www.bkjia.com/PHPjc/1060102.html www.bkjia.com true http://www.bkjia.com/PHPjc/1060102.html techarticle PHP serialize serialized data and JSON formatted data analysis, Serializejson PHP serialize is to serialize variables, return a variable type and structure of the string expression, and ...

  • Related Article

    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.