Re: passing arrays
| From: | Zak Greant | Date: | Sat, 23 Jun 2001 04:33:31 +0000 |
| Subject: | Re: passing arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-54955@lists.php.net to get a copy of this message | ||
Jason Jacobs wrote:
> Hi all. I'm trying to pass an array to another page, and I'm not having
> luck. I thought maybe it would do something slick by passing it in the
> url, but it doesn't. So, how can I pass the array?
You can pass an array between pages by using the serialize() and
unserialize() functions in conjunction with rawurlencode().
Serializing a variable is kind of like freezing food.
You freeze food so that you can store it for later use. Serializing
does roughly the same thing for a variable, converting a 'fresh'
variable into a string that you can store and then 'thaw' for later
use.
The basic steps to use serialize to transfer data between pages are:
0.) Page foo.php has an array that it wants to pass to bar.php
$array = array ('doe', 'diddy', 'dee', 'dum');
1.) Serialize the array
$serialized_array = serialize ($array);
2.) Make the serialized data safe for transport via http by passing it
to rawurlencode()
$serialized_array = rawurlencode ($serialized_array);
3.) Pass the serialized array to another page via GET. (You could also
use a form and POST)
echo "The serialized array looks like this: $serialized_array<br>";
<a href="bar.php?array=<?php echo $serialized_array ?>">Send</a>
4.) Unserialize the array at page bar.php
(You may need to call stripslashes() on the passed data - review
the magic_quotes directives in your php.ini file for more
information)
$array = unserialize (stripslashes ($array));
5.) Voilà! Garnish with a 'Powered by PHP Logo' and it is ready to
serve. ;)
Good Luck!
--zak