Passing Serialized Arrays Between PHP Pages

Fri Feb 12 05:58:43 2010

Passing arrays between pages can be a useful thing to know, particularly when you feel data is a little too long to stick inside a url and use $_GET[]. That is not to say that this is the best way of doing it. I am quite sure that there are many coders out there just shouting about a better method, or about the does and don'ts of PHP, but this, as I say, is good to know. So here goes...

First off, we need an array to pass. I will be using the one below as an example.

$arrayToSerialize = array("key1"=>"value1","key2"=>"value2","key3"=>"value3");
As you can see, it is an associative array with keys and values. To serialized the array simply call the serialized() method and pass in the array name, as in the following example. Note that I have used the variable $serializedArray to store the serialized data.

$serializedArray = serialize($arrayToSerialize);
Now we create a form to send the serialized data on a submit button click as follows.

<form action="unserialize.php" method="post">

      <input type="hidden" id="serializedArray" name="serializedArray" 
         value='<?php echo $serializedArray;?>'/>

      <input type="submit" value="Continue;">
</form>
The action of the form above points to a file we will create next called unserialize.php. The method is set to post as we will be sending the data. Inside the form, there is a hidden input which will store and pass the data on to the unserialize.php file. The name of the hidden input is set to 'serializedArray' which we will need to remember for later. Lastly, inside the form also resides the submit button that will trigger the form and send the data.



Next we need to create a file called unserialize.php as we mentioned earlier. Inside that file we write the following.

$serializedArray = $_REQUEST["serializedArray"]; 
$unserializedArray = unserialize(stripslashes($serializedArray));  
The code above captures the serialized data in a variable again called $serializedArray. Notice that the name of the hidden input inside serialize.php is placed inside a server $_REQUEST[] to retrived the data. In the line after that the data is unserialized with the unserialize() method call, passing in the $serializedArray variable. The unserialized data is them stored in another variable called $unserializedArray. The $unserializedArrayvariable can now be used as any other array. If you use print_r($unserializedArray), you will notice the results shown below are the same as the original array passed from serialized.php.



Results of print_r($unserializedArray) :

Array ( [key1] => value1 [key2] => value2 [key3] => value3 )

I have put together a quick example which you can download from the link below.

Download Serialize Array Example


← back

Most recent posts

  1. Flashcards Maker - the app the helps you learn in a flash
  2. Convert Font Suitcase to TTF in 3 Easy Steps
  3. Rain or Shine - Weather Forecast Available Now!
  4. Getting around Apple's silly rules for iOS 10
  5. Localising App Names for iOS and Android

Search site:

Apps by Ubacoda.com

Click the app icons to see more info


Archives

Categories