» » Use PHP to serialize data and JSON to formate data

 

Use PHP to serialize data and JSON to formate data

Author: bamboo06 on 16-02-2016, 23:52, views: 2633

6
PHP's serialize a sequence of the variable, the variable type and returns a string expression with a structure, but JSON is a lighter, more user-friendly format for interfaces (AJAX, REST, etc.) for data exchange. In fact, both are in a string manner to reflect a data structure. So what difference does it have between them?
Use PHP to serialize data and JSON to formate data

serialize serialization
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:
a:2:{i:0;a:4:{s:8:"gameName";s:6:"a";s:8:"homeName";s:15:"b";s:9:"guestName";s:12:"c";s:7:"endTime";s:10:"2015-08-21";}i:1;a:4:{s:8:"gameName";s:6:"d";s:8:"homeName";s:9:"e";s:9:"guestName";s:15:"f";s:7:"endTime";s:10:"2015-08-22";}} 

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.
print_r(unserialize($serialize)); 

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; 

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, 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.
print_r(json_decode($jsonencode,true)); 


summary
PHP's serialize the array is serialized for easy storage, and the data stored in JSON format is not only convenient, but also with other languages such as javascript read. They may be subtle differences in performance if more interactive use of front and rear ends, then I suggest using JSON, combined with PHP, javascript, JSON, and Ajax can complete the powerful data exchange capabilities.

Category: PHP Scripts / Javascript

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
24 August 2017 23:35

Casey Barton

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Great, Will surely keep it mind about Using PHP to serialize data and JSON to format data, I have to tell my pals about it too at http://best-essay-writing.services for all to know of it, Thank you for one perfect looking post.

<
  • 0 Comments
  • 0 Articles
22 September 2017 20:08

Gabe Hussain

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
Great post share, good to know that now we can use PHP to serialize data and JSON to formate data like commercial carpet denver. The serialize serialization method and all seems really good too, thanks.

<
  • 0 Comments
  • 0 Articles
29 September 2017 09:25

Bailey Butler

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I prefer the submit. It really is excellent to find out an individual verbalize from your coronary heart and also quality with this crucial subject matter may be effortlessly witnessed. more details see web site. Jual Laptop Murah

<
  • 0 Comments
  • 0 Articles
12 January 2018 21:04

DinaHaines

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
I love posts like this! 'A passive-aggressive way of slagging something off...' Hilarious! custom essay

<
  • 0 Comments
  • 0 Articles
19 February 2018 23:57

Mary Elbert

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
SMS Marketing is an open door, we were to a great degree eager to investigate. We have effectively composed and conveyed numerous Bulk SMS USA Service crusades for our customers and are amazingly anxious to impart to every one of the individuals who are searching for a compelling medium to contact customers inside seconds at costs that meet the financial plan.

<
  • 0 Comments
  • 0 Articles
5 March 2018 14:29

replica omega watches

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
To have a sense of rolex replica watches presence of the watch, you will think bright colorful table, a rolex replica uk bright appearance is enough to win all the attention. A black watch can also have a omega replica distinctive beauty, they do not have a beautiful appearance can also have a rolex uk strong sense of presence.

Information
Comment on the news site is possible only within (days) days from the date of publication.