...

View Full Version : Storing arrays into Session Variables



Det7
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>
etc..

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?

delinear
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:

session_start();

$_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>';
print_r($_SESSION);
echo '</pre>';
This results in:

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

)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum