Passing Array Variables using POST or GET with PHP

A convenient way to pass a single or multidimensional (possible utf-8) array is to serialize it (“convert it to a string”) and pass it using a hidden input. After post or get, unserialize string variable, so the original array will be available.

How to serialize the array

Store the value in a hidden input. Try the following:

<input type="hidden"
        id="str_var"
        name="str_var"
        value="<?php print base64_encode(serialize($array_var)) ?>">;

Unserialize (after post or get)

Use something like this

$str_var = $_POST["str_var"];
$array_var = unserialize(base64_decode($str_var));

Encode URL

In case you have to create with code (e.g. javascript) the url to get, remember to encode it as a valid url:

<input type="hidden"
        id="str_var" name="str_var"
        value="<?php print urlencode(base64_encode(serialize($array_var))) ?>">;

References

From PHP docs

Your comments are welcome!


Never Miss A Post!

Subscribe to our free NewsLetter