Most often we need to store a
complex array in the database or in a file from PHP. Some of us might have surely searched for some built-in function to accomplish this task. Complex arrays are arrays with elements of more than one data-types or array.
But, we already have a handy solution to handle this situation. We don't have to write our own function to convert the complex array to a formatted string. There are two popular methods of serializing variables.
- serialize()
- unserialize()
We can serialize any data in PHP using the serialize() function. The serialize() function accepts a single parameter which is the data we want to serialize and returns a serialized string. Below program illustrate this:
php
<?php
// a complex array
$myvar = array(
'hello',
42,
array(1, 'two'),
'apple'
);
// convert to a string
$string = serialize($myvar);
// printing the serialized data
echo $string;
?>
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";}
From the above code, we have a variable with serialized data,
$string . We can unserialize the value of the variable using
unserialize() function to get back to the original value of the
complex array,
$myvar.
Below program illustrate both serialize() and unserialize() functions:
php
<?php
// a complex array
$myvar = array(
'hello',
42,
array(1, 'two'),
'apple'
);
// serialize the above data
$string = serialize($myvar);
// unserializing the data in $string
$newvar = unserialize($string);
// printing the unserialized data
print_r($newvar);
?>
Output:
Array
(
[0] => hello
[1] => 42
[2] => Array
(
[0] => 1
[1] => two
)
[3] => apple
)
This was 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 use the
json_encode() and
json_decode() functions as well for serializing and unserializing data in PHP respectively.
Since the
JSON format is text only, it can be easily sent to and from a server and can be used as a data format by any programming language.
Lets have a look how to use
json_encode() in PHP:
php
<?php
// a complex array
$myvar = array(
'hello',
42,
array(1, 'two'),
'apple'
);
// serializing data
$string = json_encode($myvar);
// printing the serialized data
echo $string;
?>
Output:
["hello",42,[1,"two"],"apple"]
We can decode the data encoded in above program using the json_decode() function to get the original complex array. Below program illustrate this:
php
<?php
// a complex array
$myvar = array(
'hello',
42,
array(1, 'two'),
'apple'
);
// serializing data
$string = json_encode($myvar);
// decoding the above encoded string
$newvar = json_decode($string);
// printing the decoded data
print_r($newvar);
?>
Output:
Array
(
[0] => hello
[1] => 42
[2] => Array
(
[0] => 1
[1] => two
)
[3] => apple
)
Note: JSON encoding and decoding is more compact, and best of all, compatible with javascript and many other languages. However, for complex objects, some information may be lost.
Similar Reads
PHP Data Types
In PHP, data types define the kind of value a variable can hold. PHP is a loosely typed language, meaning you donât need to declare the data type of a variable. It is automatically assigned based on the value. But it is important to understand data types because it is important for writing reliable,
4 min read
PHP | ArrayIterator serialize() Function
The ArrayIterator::serialize() function is an inbuilt function in PHP which is used to serialize the array iterator. Syntax: string ArrayIterator::serialize( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the serialized ArrayIterator. Below progr
1 min read
PHP | ArrayObject serialize() Function
The ArrayObject::serialize() function is an inbuilt function in PHP which is used to serialize the ArrayObject. Syntax: string ArrayObject::serialize( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the serialized representation of the ArrayObject
1 min read
PHP | ArrayIterator unserialize() Function
The ArrayIterator::unserialize() function is an inbuilt function in PHP which is used to unserialize the serialize object. Syntax: void ArrayIterator::unserialize( string $serialized ) Parameters: This function accepts single parameter $serialized which holds the serialize array iterator object. Ret
2 min read
PHP String Functions
Strings are a fundamental data type in PHP, used to store and manipulate text. PHP provides a wide variety of built-in string functions. These functions perform various operations such as string transformations, character manipulations, encoding and decoding, and formatting, making string handling s
6 min read
PHP | ArrayObject unserialize() Function
The ArrayObject::unserialize() function is an inbuilt function in PHP which is used to unserializes the serialized ArrayObject. Syntax: void ArrayObject::unserialize( string $serialized ) Parameters: This function accepts single parameter $serialized which holds the serialized ArrayObject. Return Va
1 min read
PHP | Format Specifiers
Strings are one of the most used Datatype arguably irrespective of the programming language. Strings can be either hardcoded (specified directly by the developer) or formatted (where the basic skeleton is specified and the final string is obtained by incorporating values of other variables). Formatt
5 min read
PHP | SplDoublyLinkedList serialize() Function
The SplDoublyLinkedList::serialize() function is an inbuilt function in PHP which is used to serializes the storage. Syntax: string SplDoublyLinkedList::serialize( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the serialized string. Below progra
1 min read
Variables and Datatypes in PHP
A variable in PHP is a container used to store data. The value of a variable can change during program execution. PHP variables start with a dollar sign ($), followed by the variable name.$name = "GFG";$age = 30;Declaring Variables in PHPTo declare a variable, you simply use the dollar sign followed
4 min read
PHP SplObjectStorage serialize() Function
The SplObjectStorage::serialize() function is an inbuilt function in PHP which is used to serialize the result of the storage. Syntax: string SplObjectStorage::serialize() Parameters: This function does not accept any parameter. Return Value: This function returns a string which is the representatio
1 min read