search
HomeBackend DevelopmentPHP TutorialPHP 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1060102.htmlTechArticlePHP’s serialize serialized data and JSON formatted data analysis, serializejson PHP’s serialize is to serialize variables and return A string expression with variable type and structure, and...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software