I'm looking for the name of the PHP function to build a query string from an array of key value pairs. Please note, I am looking for the built in PHP function to do this, not a homebrew one (that's all a google search seems to return). There is one, I just can't remember its name or find it on php.net. IIRC its name isn't that intuitive.
You're looking for http_build_query()
.
-
-
14Be careful with this function! It will omit any key-value pair where the value is NULL.
echo http_build_query(array("foo"=>"bar","bar"=>null))
will produce onlyfoo=bar
– cb0 Aug 13 '14 at 13:06 -
@cb0 this works similar to form submits in a browser, an empty input field will not be included in the submitted query. – TJ L Aug 5 '16 at 17:28
-
1@ceejayoz Well you've known it for a long time now... Your wish has come true? – Andrew Nov 30 '16 at 19:56
Here's a simple php4-friendly implementation:
/**
* Builds an http query string.
* @param array $query // of key value pairs to be used in the query
* @return string // http query string.
**/
function build_http_query( $query ){
$query_array = array();
foreach( $query as $key => $key_value ){
$query_array[] = urlencode( $key ) . '=' . urlencode( $key_value );
}
return implode( '&', $query_array );
}
-
30
-
1When doing its decoding, PHP appears to percent-decode the key as well. Possibly worth doing that here? – cloudfeet Aug 1 '13 at 15:38
-
Just as addition to @thatjuan
's answer.
More compatible PHP4 version of this:
if (!function_exists('http_build_query')) {
if (!defined('PHP_QUERY_RFC1738')) {
define('PHP_QUERY_RFC1738', 1);
}
if (!defined('PHP_QUERY_RFC3986')) {
define('PHP_QUERY_RFC3986', 2);
}
function http_build_query($query_data, $numeric_prefix = '', $arg_separator = '&', $enc_type = PHP_QUERY_RFC1738)
{
$data = array();
foreach ($query_data as $key => $value) {
if (is_numeric($key)) {
$key = $numeric_prefix . $key;
}
if (is_scalar($value)) {
$k = $enc_type == PHP_QUERY_RFC3986 ? urlencode($key) : rawurlencode($key);
$v = $enc_type == PHP_QUERY_RFC3986 ? urlencode($value) : rawurlencode($value);
$data[] = "$k=$v";
} else {
foreach ($value as $sub_k => $val) {
$k = "$key[$sub_k]";
$k = $enc_type == PHP_QUERY_RFC3986 ? urlencode($k) : rawurlencode($k);
$v = $enc_type == PHP_QUERY_RFC3986 ? urlencode($val) : rawurlencode($val);
$data[] = "$k=$v";
}
}
}
return implode($arg_separator, $data);
}
}
but for inverse this work, you can use :
void parse_str(str $input, array $output);
//for example:
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
Good luck.
As this question is quite old and for PHP, here is a way to do it in the (currently) very popular PHP framework Laravel.
To encode the query string for a path in your application, give your routes names and then use the route()
helper function like so:
route('documents.list.', ['foo' => 'bar']);
The result will look something like:
http://localhost/documents/list?foo=bar
Also be aware that if your route has any path segment parameters e.g. /documents/{id}
, then make sure you pass an id
argument to the route()
parameters too, otherwise it will default to using the value of the first parameter.
Implode will combine an array into a string for you, but to make an SQL query out a kay/value pair you'll have to write your own function.
-
Tried that but it won't work. I'm trying to build an http query string which requires both the keys and the values from the array, implode can't do this. – Robin Barnes Dec 30 '08 at 17:00
-
2I see, wasn't sure if you meant an SQL query string or a http query string. – Click Upvote Dec 30 '08 at 21:05