Re: Passing arrays
| From: | Matt McClanahan | Date: | Tue, 23 May 2000 17:10:44 +0000 |
| Subject: | Re: Passing arrays | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-68@lists.php.net to get a copy of this message | ||
On Tue, 23 May 2000, Mick Lloyd wrote:
> I'm still struggling to understand why I can't send a query result to be
> processed in another script. In the code below both print routines work fine
> in the same script file - the first using $row and the second using
> $profilesarray. In 'aa.phtml' however, printing the first five elements of
> $profilesarray displays:
>
> A;r;r;a;y;
>
> Why can't I or how can I pass an array to a second file?
Due to restrictions in how passing variables with HTTP works, you can only
pass a one-dimensional array directly. However, the variable you're
trying to pass is a 2-d array (That is, it's an array of strings)
One solution is to serialize the array before passing it, and to
unserialize it on the other side. For example:
<?php
if( isset($ser1) )
{
$arr1 = urldecode($ser1);
$arr1 = unserialize($arr1);
echo 'First four elements of array:';
echo $arr1[0] . '<br>' . $arr1[1] . '<br>' . $arr1[2] .
'<br>' .
$arr1[3] . '<br>';
}
else
{
$arr[] = 'First';
$arr[] = 2;
$arr[] = 'Third';
$arr[] = 4;
$ser = serialize($arr);
$ser = urlencode($ser);
echo '<a href="' . $PHP_SELF . '?ser1=' . $ser .
'">Pass</a>';
}
?>
By serializing and urlencoding (Which makes it safe to transport over
HTTP), the multidimensional array is just a long, nonsensical string as
far as HTTP cares.
HTH,
Matt