Amit Dhamu

Software Engineer

Serialize and Unserialize Array

Sometimes, it may be necessary to pass an array to another file via GET (although not normally a recommended practice). To do this, you need to pass the array as a URL safe string or as a serialized array. You can do this as below.

$test = [];
$test[1] = 'orange';
$test[2] = 'apple';
$test[3] = 'tomato';
$test[4] = 'grapes';
$test[5] = 'melon';
$test[6] = 'strawberry';

// print_r($test) will output
Array
(
    [1] => orange
    [2] => apple
    [3] => tomato
    [4] => grapes
    [5] => melon
    [6] => strawberry
)

// print_r(serialize($test)) will output
a:6:{i:1;s:6:"orange";i:2;s:5:"apple";i:3;s:6:"tomato";i:4;s:6:"grapes";i:5;s:5:"melon";i:6;s:10:"strawberry";}

// print_r(unserialize($test)) will output
Array
(
    [1] => orange
    [2] => apple
    [3] => tomato
    [4] => grapes
    [5] => melon
    [6] => strawberry
)

 array / php / serialize / unserialize / GET

Prompt For User InputViewport Meta Tag For Mobile

Show Comments

Made with by Amit Dhamu.
© MMXX. All rights reserved.