How does PHP serialize data?

藏色散人
Release: 2023-04-05 12:00:02
Original
5543 people have browsed it

Most of the time, we need to store complex arrays in a database or in a file in PHP. Some people may use PHP built-in functions to accomplish this task. A complex array is an array that has elements of multiple data types or arrays.

How does PHP serialize data?

However, we already have a convenient solution to handle this situation. We don't have to write our own function to convert complex arrays into formatted strings. There are two popular methods of variable serialization. (Recommended: PHP Tutorial)

1、serialize()

2、unserialize()

We can use the serialize() function to serialize any data in PHP. The serialize() function accepts a parameter, which is the data we want to classify and returns a serialized string.

The program is as follows:

<?php 
  
//一个复杂的数组
$myvar = array( 
    &#39;hello&#39;, 
    42, 
    array(1, &#39;two&#39;), 
    &#39;apple&#39;
); 
  
// 转换成字符串 
$string = serialize($myvar); 
  
// 打印序列化的数据
echo $string; 
  
?>
Copy after login

Output:

a:4:{i:0;s:5:"hello";i:1;i:42;i:2;a:2:{i:
0;i:1;i:1;s:3:"two";}i:3;s:5:"apple";}
Copy after login

From the above code, we get a variable $string with serialized data. We can use the unserialize() function to deserialize the value of a variable to return the original value of the complex array $myvar.

serialize() and unserialize() function example:

<?php
  
// 一个复杂的数组
$myvar = array(
    &#39;hello&#39;,
    42,
    array(1, &#39;two&#39;),
    &#39;apple&#39;
);
  
// 序列化上述数据
$string = serialize($myvar);
  
// 反序列化$string中的数据
$newvar = unserialize($string);  
   
// 打印未序列化的数据
print_r($newvar);
  
?>
Copy after login

Output:

Array
(
    [0] => hello
    [1] => 42
    [2] => Array
        (
            [0] => 1
            [1] => two
        )

    [3] => apple
)
Copy after login

This is the native PHP serialization method. However, since JSON has become so popular in recent years, they decided to add support for it in PHP 5.2. Now you can serialize and deserialize data in PHP using json_encode() and json_decode() functions respectively.

Since the JSON format is text only, it can be easily sent to or from the server and can be used as a data format by any programming language.

Let’s see how to use json_encode() in PHP :

<?php 
  
// 一个复杂的数组
$myvar = array( 
    &#39;hello&#39;, 
    42, 
    array(1, &#39;two&#39;), 
    &#39;apple&#39;
); 
  
// 序列化数据
$string = json_encode($myvar); 
  
// 打印序列化的数据
echo $string; 
  
?>
Copy after login

Output:

["hello",42,[1,"two"],"apple"]
Copy after login

We can use json_decode() Function decodes the data encoded in the above program to obtain the original complex array.

The program is as follows:

<?php 
  
// 一个复杂的数组
$myvar = array( 
    &#39;hello&#39;, 
    42, 
    array(1, &#39;two&#39;), 
    &#39;apple&#39;
); 
  
// 序列化数据
$string = json_encode($myvar); 
  
// 解码上面编码的字符串
$newvar = json_decode($string); 
   
// 打印解码后的数据
print_r($newvar); 
  
?>
Copy after login

Output:

Array
(
    [0] => hello
    [1] => 42
    [2] => Array
        (
            [0] => 1
            [1] => two
        )
    [3] => apple
)
Copy after login

The above is the detailed content of How does PHP serialize data?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!