php PHP函数从数组构建查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/400805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP function to build query string from array
提问by Robin Barnes
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 PHPfunction 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.
我正在寻找 PHP 函数的名称,以从键值对数组构建查询字符串。请注意,我正在寻找内置的 PHP函数来执行此操作,而不是自制函数(这似乎是谷歌搜索返回的全部)。有一个,我就是不记得它的名字或在 php.net 上找到它。IIRC 它的名字不是那么直观。
回答by TJ L
You're looking for http_build_query().
您正在寻找http_build_query().
回答by 0x6A75616E
Here's a simple php4-friendly implementation:
这是一个简单的 php4 友好的实现:
/**
* 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 );
}
回答by vp_arth
Just as addition to @thatjuan's answer.
More compatible PHP4 version of this:
就像对@thatjuan的回答一样。
更兼容的 PHP4 版本:
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);
}
}
回答by alexkb
As this question is quite old and for PHP, here is a way to do it in the (currently) very popular PHP framework Laravel.
由于这个问题已经很老了,而且对于 PHP,这里有一种在(当前)非常流行的 PHP 框架 Laravel 中的方法。
To encode the query string for a path in your application, give your routes namesand then use the route()helper functionlike so:
要编码应用程序中路径的查询字符串,请提供路由名称,然后像这样使用route()辅助函数:
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 idargument to the route()parameters too, otherwise it will default to using the value of the first parameter.
另请注意,如果您的路线有任何路径段参数,例如/documents/{id},那么请确保您也将id参数传递给route()参数,否则将默认使用第一个参数的值。

