Re: arrays
| From: | Philip Olson | Date: | Sun, 26 May 2002 18:06:13 +0000 |
| Subject: | Re: arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-99307@lists.php.net to get a copy of this message | ||
> Can arrays be passed to functions just like a simple variable?
Yes.
> Can arrays be passed as values in hidden form fields just like a
> simple variable?
Yes, with a little work.
> I've been playing around with these and getting inconsistent
> results. I've been trying things like serialize and urlencode,
> but still haven't worked out a straightforward 'rule' or policy.
For example:
$arr = array('color' => 'red', 'fruit' => 'apple');
$str_arr = urlencode(serialize($arr));
<input type="hidden" name="foo" value="<?php echo $str_arr
?>">
And in the action script, do:
$arr = unserialize(stripslashes($_REQUEST['foo']));
Notes:
a) stripslashes may be needed, in case magical quotes
get in the way (magic_quotes_gpc).
b) am using $_REQUEST, you may use $_GET or $_POST or
whatever contains the form value.
c) hidden, text, textarea ... doesn't matter.
d) a way to test if what _should_ be an array
is really an array is with print_r() and/or
is_array(). print_r($arr);
e) we don't need urldecode() because that'll
happen automatically when going through the form.
f) you could use implode() instead of serialize too.
> Can someone throw some light on this?
I hope so :)
Regards,
Philip Olson