note 72911 deleted from function.http-build-query by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:37:02 +0000 |
| Subject: | note 72911 deleted from function.http-build-query by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189774@lists.php.net to get a copy of this message | ||
Note Submitter: mqchen at gmail dot com
----
To flyingmeteor,
Your function is pleasingly adequate, however, when comparing the results from your function with
the results from the actual function, it has a minor defect. If one uses a special character (e.g.
øæåöä) in a key in the $data array, it is not encoded by your function, but it is by
the actual one.
This is easily solved by some minor edits:
<?php
if(!function_exists('http_build_query')) {
function http_build_query($data,$prefix=null,$sep='',$key='') {
$ret = array();
foreach((array)$data as $k => $v) {
$k = urlencode($k);
if(is_int($k) && $prefix != null) {
$k = $prefix.$k;
};
if(!empty($key)) {
$k = $key."[".$k."]";
};
if(is_array($v) || is_object($v)) {
array_push($ret,http_build_query($v,"",$sep,$k));
}
else {
array_push($ret,$k."=".urlencode($v));
};
};
if(empty($sep)) {
$sep = ini_get("arg_separator.output");
};
return implode($sep, $ret);
};
};
?>
Forgive my personal coding standard.