Re: arrays
| From: | Michael Virnstein | Date: | Sun, 26 May 2002 15:07:04 +0000 |
| Subject: | Re: arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-99299@lists.php.net to get a copy of this message | ||
> Can arrays be passed to functions just like a simple variable?
yes, no difference. You can pass every variable to a function, you can pass
constants or your values directly. You have to use a variable if a function
requires passing by reference or if you want to pass by refernce, because
only variables can take the value back.
> Can arrays be passed as values in hidden form fields just like a
> simple variable?
yes, but differently.
either you serialize the array, send it urlencoded to the page and
unserialze it there:
//page1.php:
<?php
$array = array(1,2,3,4,5);
$array = urlencode(serialize($array));
?>
<a href="page2.php?array=<?php echo $array; ?>">Page2</a>
//or input field:
<input type="hidden" name="array" value="<?php echo $array;
?>">
//page2.php
<?php
$array = unserialize($array);
print_r($array);
?>
or you can appen urls like this, no serialization or sth needed then both
pages:
numerical indexed arrays:
<a
href="page2.php?array[]=1&array[]=2&array[]=3&array[]=4&array[]=5">Page2</a>
or
<a
href="page2.php?array[0]=1&array[1]=2&array[2]=3&array[3]=4&array[4]=5">Page
2</a>
string indexed arrays:
<a
href="page2.php?array[test1]=1&array[test2]=2&array[test3]=3&array[test4]=4&
array[test5]=5">Page2</a>
or with forms:
numerical indexed arrays:
<input type="hidden" name="array[]" value="1">
<input type="hidden" name="array[]" value="2">
<input type="hidden" name="array[]" value="3">
<input type="hidden" name="array[]" value="4">
<input type="hidden" name="array[]" value="5">
or
<input type="hidden" name="array[0]" value="1">
<input type="hidden" name="array[2]" value="2">
<input type="hidden" name="array[3]" value="3">
<input type="hidden" name="array[4]" value="4">
<input type="hidden" name="array[5]" value="5">
string indexed arrays:
<input type="hidden" name="array[test1]" value="1">
<input type="hidden" name="array[test2]" value="2">
<input type="hidden" name="array[test3]" value="3">
<input type="hidden" name="array[test4]" value="4">
<input type="hidden" name="array[test5]" value="5">
Michael
"Michael Hall" <olc@ninti.com> schrieb im Newsbeitrag
news:Pine.LNX.4.21.0205262226310.740-100000@localhost.localdomain...
>
> A couple of simple ones here ... the usual references don't appear to give
> a straightforward answer on these:
>
> Can arrays be passed to functions just like a simple variable?
>
> Can arrays be passed as values in hidden form fields just like a
> simple variable?
>
> 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.
>
> Can someone throw some light on this?
>
> TIA
> Michael
>