note 54439 deleted from function.urlencode by peehaa
| From: | peehaa@php.net | Date: | Fri, 06 Jan 2017 15:46:27 +0000 |
| Subject: | note 54439 deleted from function.urlencode by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208687@lists.php.net to get a copy of this message | ||
Note Submitter: linus at flowingcreativity dot net
----
I just came across the need for a function that exports an array into a query string. Being able to
use urlencode($theArray) would be nice, but here's what I came up with:
<?php
function urlencode_array(
$var, // the array value
$varName, // variable name to be used in the query string
$separator = '&' // what separating character to use in the query string
) {
$toImplode = array();
foreach ($var as $key => $value) {
if (is_array($value)) {
$toImplode[] = urlencode_array($value, "{$varName}[{$key}]", $separator);
} else {
$toImplode[] = "{$varName}[{$key}]=".urlencode($value);
}
}
return implode($separator, $toImplode);
}
?>
This function supports n-dimensional arrays (it encodes recursively).