Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of serialize Serialization data and JSON format data of PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "PHP serialize serialization data and JSON formatted data analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "PHP serialize serialization data and JSON formatted data analysis"!

Serialize serialization

In some old WEB systems, we may see a large string of string text content that seems to have a special meaning stored in a database or in a 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 the array. Let's assume that there is an array like this:

$arr = array ("0" = > array ("gameName" = > "Germany", "homeName" = > "Bielefeld", "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 contents of this array in a database or text file so that it can be read elsewhere.

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

We serialize the array using PHP's serialize to output the following result:

GuestName 2: "guestName"; SPLR 12: "Brunswick"; SPLV 7: "endTime"; SPL10: "2015-08-21";} iRank 1: 4: {SJV 8: "gameName"; Srig 6: "Premier League"; Srig 8: "homeName" SRV 9: "Crystal Palace"; Srig 9: "guestName"; SRV 15: "Aston Villa"; SRV 7: "endTime"; SRV 10: "2015-08-22";}}

The result of the above output looks complex, but it is actually very simple, it shows some data types and structures.

ARanger 2 indicates that this is an array with two elements (array)

IRank 0 refers to sequential index

ARV 4 means there are 4 fields.

SRAR8: "gameName" indicates that this is an 8-character string (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 into an array, you can use the unserialize () function.

Print_r (unserialize ($serialize))

The above code can print out an array.

JSON data parsing

We know that the PHP operation JSON can use two functions, json_encode () and json_decode (). 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, which is easy to 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, after using JSON, the data space is less than serialize, and the Chinese string in the output result is encoded, so it is easy to identify manually, and the key is that the data in JSON format is convenient for other languages to read and identify, so some people say that it is a substitute for XML. The data in JSON format can communicate asynchronously 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))

At this point, I believe that everyone on the "PHP serialize serialization data and JSON formatted data analysis" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report