Home > Backend Development > PHP Tutorial > PHP serialize array

PHP serialize array

王林
Release: 2024-08-29 12:45:46
Original
877 people have browsed it

The PHP serialize array function is used to serialize the given array and convert a value’s storable representation. The serialize array function is a built-in function in PHP. The serialization of data means converts a value into a sequence of bits to be stored in a memory buffer, in a file, or transfer across a network. The array is complex data types; we can not see its content directly. The serialize() function converts an array to a simple string which we can save in the file and transfer across the network in URL etc.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax of the serialize array function –

serialize(array);

Parameters –

  • array – This is not an optional parameter that specifies the array or value to be serialized or converted into the storable representation.
  • Return value – The return value of this method is a string, which is a byte-stream representation of the array that can be a store or send.

Working of PHP serialize array function

The PHP serialize() array function accepts the one parameter as ( array/value ), which is required parameter. Suppose we have an array (1, 2, 3, 4) which we want to store in the file, so first we need to serialize it by calling the function as serialize(array), which serialized the array and return the string conversion of the array as “a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}”, farther which we can store in the file or send over the network.

Examples for the PHP serialize() array function

Here are the following examples mention below

Example #1

Examples for the serialize() array function to serialize the complex array –

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, as below –

Code:

<?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 );
?>
Copy after login

Output:

PHP serialize array

As in the above code, the complex array is created and serialized using the serialize() function, which returned the byte stream or string conversion of the array. Farther, the serialized array is converted back to the array by using the unserialize() function, which is the same as the original array, as we can see in the above output.

Example #2

Examples for the serialize() array function to serialize the array and store it into the file –

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 –

Code:

<?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>");
?>
Copy after login

Output:

PHP serialize array

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

PHP serialize array

As in the above code, the complex array is created and serialized; the serialized array is stored permanently in the text file. Farther, the stored serialized array is read back and converted to the array by using the unserialize() function, which is the same as the original array, as we can see in the above output.

Example #3

Examples for the serialize() array function to serialize the array and encode –

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 –

Code:

<?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>");
?>
Copy after login

Output:

PHP serialize array

As in the above code, the complex array is created and serialized; the serialized array is encoded by using the urlencode() function. Farther, the encoded serialized array is decoded to the array by using the urldecode() function, which is the same as the original array, as we can see in the above output.

Conclusion

The PHP Zip files are used to stores the bunch of files or and directory together in the compressed form, which later can also be unzipped.

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

Related labels:
php
source:php
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