Passing php array variables through POST or GET

Have you ever wonder if there was a way to pass variables inside an array to a POST or GET without having to pass one by one?
Or even to keep its array structure on the other side?
Here is a small trick that can make this possible. Here we will use the in built php function called serialize.
What this function does is to place the array in a single string ready to be passed.

Here is a little php exercise that you can try using serialize

$example = array(”apple”,”orange”, “raspberry”, “banana”);

Here is the structure of the array

Array
(
    [0] => apple
    [1] => orange
    [2] => raspberry
    [3] => banana
)
Then when we serialized the data

$serial = serialize($example)

The content of $serial become

a:4:{i:0;s:5:”apple”;i:1;s:6:”orange”;i:2;s:9:”raspberry”;i:3;s:6:”banana”;}

and to get the array back we just used the function unserialize
$original = unserialize($serial);

and the content of $original become

Array
(
    [0] => apple
    [1] => orange
    [2] => raspberry
    [3] => banana
)

this is exactly the array that we have in the beginning.
So if you want to pass the structure of that array as well as its value, just pass the value of $serial and you can retrieve it with unserialize.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Wists
  • MisterWong
  • DZone
  • Technorati
  • YahooMyWeb

About this entry