
PHP序列化数组简介
PHP的序列化数组函数用于序列化给定的数组并转换一个值的可存储表示。序列化数组函数是PHP中的一个内置函数。数据的序列化意味着将一个值转换为一个比特序列,以存储在内存缓冲区、文件中,或在网络上传输。数组是复杂的数据类型;我们不能直接看到它的内容。序列化()函数将数组转换为简单的字符串,我们可以将其保存在文件中,并在网络上以URL等方式传输。
序列化数组函数的语法是 -
serialize(array)。
参数 -
- **array -**这不是一个可选的参数,它指定了要被序列化或转换为可存储表示的数组或值。
- **返回值 -**这个方法的返回值是一个字符串,它是数组的字节流表示,可以是一个存储或发送。
PHP序列化数组函数的工作原理
PHP serialize()数组函数接受一个参数(数组/值),它是必需的参数。假设我们有一个数组(1,2,3,4),我们想把它存储在文件中,所以首先我们需要通过调用函数serialize(array)来序列化它,它序列化数组并返回数组的字符串转换为 "a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}",然后我们可以存储在文件或通过网络发送。
PHP serialize()数组函数的例子
下面是下面提到的例子
例子 #1
序列化()数组函数序列化复杂数组的例子--#1
接下来,我们编写PHP代码,通过下面的例子更清楚地了解serialize()数组函数,其中serialize()函数被用来序列化给定的数组,如下所示
代码。
<?php // create complex array $array = array( 'text', 200, 400, 'apple', array( 2, 'two', 3, 'three' ) ); // printing complex array print("The complex array is : "); print_r( $array ); print( "<br>"); // serialize the complex array $array_string = serialize( $array ); // printing the serialize array print( "The serialize array is : " ); print( $array_string ); print( "<br>"); // unserializing the serialized array $unser_array = unserialize( $array_string ); // printing the unserialized array print_r( $unser_array ); ?>
输出。

在上面的代码中,复杂的数组被创建并使用serialize()函数进行序列化,该函数返回数组的字节流或字符串转换。更远处,通过使用unserialize()函数将序列化的数组转换回数组,这与原来的数组是一样的,我们可以在上面的输出中看到。
例子#2
序列化()数组函数将数组序列化并存储到文件中的例子 -
Next, we write the PHP code to understand the serialize() array function more clearly with the following example, where the serialize() function is used to serialize the given array and store it permanently into the text file, as below -
代码。
<?php // create an array $array = array( 'apple', 'banana', 'mango', 'orange', 'cherry' ); // printing complex array print("The array is : "); print_r( $array ); print( "<br>"); // serialize the complex array $array_string = serialize( $array ); // printing the serialize array print( "The serialize array is : " ); print( $array_string ); print( "<br>"); // save the array string to a Ex text file file_put_contents('Ex.txt', $array_string); // access back the data from the save text file. $file_array = file_get_contents('Ex.txt'); // printing the file array print( "The serialize file array is : " ); print( $file_array ); print( "<br>"); // unserializing the serialized array $unser_array = unserialize( $file_array ); // printing the unserialized array print( "The unserialize file array is : " ); print_r( $unser_array ); print( "<br>"); ?>
输出。

If we check the Ex.txt file, the content is -

在上面的代码中,复杂的数组被创建并序列化;序列化后的数组被永久地存储在文本文件中。更远处,存储的序列化数组被读回,并通过使用unserialize()函数转换为数组,这与原始数组是一样的,我们可以在上面的输出中看到。
例子#3
序列化()数组函数对数组进行序列化和编码的例子 -
Next, we write the PHP code to understand the serialize() array function, where the serialize() function is used to serialize the given array and encode it into the URL to send across the multiple pages, as below -
代码。
<?php // create an array $array = array ( 1 => "one", 2 => "two", 3 => "three" ); // printing an array print("The array is : "); print_r( $array ); print( "<br>"); // serialize the complex array $array_string = serialize( $array ); // printing the serialize array print( "The serialize array is : " ); print( $array_string ); print( "<br>"); // encode the array string $enc_array = urlencode( $array_string ); print( "The encode serialize array is : " ); print( $enc_array ); print( "<br>"); // deencode the encoded array string $dec_array = urldecode( $enc_array ); // printing the file array print( "The deencode serialized array is : " ); print( $dec_array ); print( "<br>"); ?>
输出。

如上面的代码,复杂的数组被创建并序列化;序列化的数组通过使用urlencode()函数进行编码。更远的地方,通过使用urldecode()函数将编码后的序列化数组解码为数组,这与原始数组是一样的,我们可以在上面的输出中看到。
结论
PHP Zip文件是用来将一堆文件或目录以压缩的形式存储在一起,之后也可以解压。
推荐文章
这是一个关于PHP序列化数组的指南。在这里,我们讨论了PHP序列化数组函数的工作和例子,以及代码和输出。你也可以看看下面的文章来了解更多
The postPHP serialize arrayappeared first onEDUCBA.