


PHP serialize serialized data and JSON formatted data analysis, serializejson_PHP tutorial
PHP’s serialize serialized data and JSON formatted data analysis, serializejson
PHP’s serialize serializes variables and returns a string expression with variable type and structure , while JSON is a lighter and more friendly format for data exchange through interfaces (AJAX, REST, etc.). In fact, both of them embody a data structure in the form of a string. So what's the difference between them?
serialize
In some old WEB systems, we may see a large string of string text content that seems to have special meanings stored in the database or in the 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 arrays. Let's assume there is an array like this:
$arr = array( "0" => array( "gameName" => "德乙", "homeName" => "比勒费尔德", "guestName" => "不伦瑞克", "endTime" => "2015-08-21" ), "1" => array( "gameName" => "英超", "homeName" => "水晶宫", "guestName" => "阿斯顿维拉", "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 use PHP's 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:"比勒费尔德";s:9:"guestName";s:12:"不伦瑞克";s:7:"endTime";s:10:"2015-08-21";}i:1;a:4:{s:8:"gameName";s:6:"英超";s:8:"homeName";s:9:"水晶宫";s:9:"guestName";s:15:"阿斯顿维拉";s:7:"endTime";s:10:"2015-08-22";}}
The above output result looks complicated, but it is actually very simple. It describes some data types and structures.
a:2 indicates that this is an array with two elements;
i:0 refers to the sequence index;
a:4 means there are 4 fields
s:8:"gameName" indicates that this is an 8-character string
In actual development, we only store the serialized data and do not care about the storage format and field meaning. If you want to restore the serialized data into an array, use the unserialize() function.
print_r(unserialize($serialize));
The above code can print out an array.
JSON data parsing
We know that PHP can use the json_encode() and json_decode() functions to operate JSON. json_encode() can convert an array into text data in json format, which is convenient for storage and reading, while json_decode() can directly convert json data into an array for easy 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 using JSON, the data space is less than that of serialize. The Chinese strings in the output results are encoded. If you look closely, they correspond to key values, which is convenient for manual identification. The key is that the data in JSON format is easy to read in other languages. And recognized, so some people say it is an XML replacement. Data in JSON format can complete an asynchronous interaction process 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));
Students who are interested in the application of JSON can refer to the article on this site: Application of JSON in PHP
Summary
PHP's serialize serializes arrays for easy storage, and data in JSON format is not only easy to store but can also be read with other languages such as javascript. They may have subtle differences in performance. If there is a lot of front-end and back-end interaction, it is recommended to use JSON. Combining PHP, Javascript, JSON and Ajax can complete powerful data interaction functions.
That’s it for the detailed analysis of PHP’s serialize data and JSON formatted data. If you want to know more, please continue to pay attention.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
