note 16542 deleted from function.urlencode by jimw
| From: | jimw@php.net | Date: | Sat, 03 Nov 2001 18:25:28 +0000 |
| Subject: | note 16542 deleted from function.urlencode by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-20390@lists.php.net to get a copy of this message | ||
if you need to pass an array variable or an array of variables, you can use the function below.
Examples:
<?
header("Location: http://example.com/something.php?" .
myurlencode($HTTP_POST_VARS));
?>
or
<?
$vars=array("blah"=>array(1,2,3));
echo '<a href="something.php?'.myurlencode($vars).'">Click
here</a>';
// output: <a href="something.php?blah[0]=1&blah[1]=2&blah[2]=3">Click
here</a>
?>
----
function myurlencode($a)
{
$out="";
$queue=array();
foreach($a as $key=>$val) {
if (!is_array($val)) {
if (strlen($out)) $out.="&";
$out.=urlencode($key);
$out.="=";
$out.=urlencode($val);
} else {
$queue[]=array($key, $val);
}
}
while (sizeof($queue)) {
list($key,$val)=array_shift($queue);
foreach($val as $field => $fvalue) {
if (is_array($fvalue)) {
$queue[]=array($key."[".$field."]", $fvalue);
} else {
if (strlen($out)) $out.="&";
$out.=$key."[".$field."]=".urlencode($fvalue);
}
}
}
return $out;
}