View Full Version : Storing arrays into Session Variables

07-01-2005, 09:29 AM
I know this is possible by using Serialize and unserialize functions. But I just can't understand its logic. I mean.. I saw an example of a session variable having a couple of values, that is through a select multiple form..

<select name = products[] multiple>

then all values that the user selected in the combo box will apear in the session variable after he has transferred the data from the products[] to the serialize($_SESSION[products])..

now what i would like to happen is storing an array into a session variable. The data will not be from a combo box.. but rather from different $_POST variables..

can someone give me a sample code?

07-01-2005, 12:40 PM
You don't really need to serialize the data at all unless you have a specific reason to do so. If not, sessions can be viewed almost as arrays themselves and as such you can turn them into multi-dimensional arrays and treat them pretty much as you would any other array.

For instance:


$_POST['name'] = 'Bob';
$_POST['email'] = 'bob@domain.com';
$_POST['address'] = 'my house';

$_SESSION['form_results'] = array('name' => $_POST['name'], 'email' => $_POST['email'], 'address' => $_POST['address']);

echo '<pre>';
echo '</pre>';
This results in:

[form_results] => Array
[name] => Bob
[email] => bob@domain.com
[address] => my house