Jump to content

Help with sending an array variable from page to page


Darkmatter5

Recommended Posts

I've heard to accomplish sending array variables from page to page in PHP, you need to use serialize() and unserialize().  Is this try?  Here's my example.

 

Imagine in one page I generate an array $names[].  It contains a list of names obviously.  How do I use serialize() and unserialize() to pass this array from page to page so I can later use a loop to display each element of the array?

 

Thanks!

$serialized = serialize($names);
$unserialized = unserialize($serialized);

 

Another option would be storing the _POST values:

$_SESSION['_POST'] = array_merge($_SESSION['_POST'], $_POST);//merges the new _POST with the already existing values. Note: if key exist in _SESSION[_POST] it will be overwritten

Yes, that is a good way to store arrays (if you only need the array as is) although you will still need to find a way to pass the variable between the pages. Either by posting a form, saving it in a session or cookie or storing it in a text file or a database. Which alternative you would want to use depends on the situation of course.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.