php 使用数组中的参数生成 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13276226/
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
Generate URL with parameters from an array
提问by Motive
I need to take an array like I have below:
我需要采用如下所示的数组:
$subids = Array
(
[s1] => one
[s2] => two
[s3] => three
[s4] => four
[s5] => five
[s6] => six
)
and generate a URL such as http://example.com?s1=one&s2=two&s3=three=&s4=four&s5=five&s6=six
并生成一个 URL,例如http://example.com?s1=one&s2=two&s3=three=&s4=four&s5=five&s6=six
All of the subids are not always defined, so sometimes s3 may not be defined, so it shouldn't be appended to the URL. Additionally, whatever the first subid is, it has to have the ? preceding it instead of the ampersand (&)
并非总是定义所有 subid,因此有时可能未定义 s3,因此不应将其附加到 URL。此外,无论第一个 subid 是什么,它都必须具有 ? 在它前面而不是与号 (&)
So if the array is just:
所以如果数组只是:
$subids = Array
(
[s2] => two
[s6] => six
)
then the URL needs to be http://example.com?s2=two&s6=six
那么 URL 需要是http://example.com?s2=two&s6=six
I have the following so far:
到目前为止,我有以下几点:
$url = 'http://example.com'
$url = ' http://example.com'
foreach ($subids AS $key => $value) {
$result[$id]['url'] .= '&' . $key . '=' . $value;
}
However, I'm not sure what the best way would be to append the ? at the beginning of the first key/value pair.
但是,我不确定附加 ? 在第一个键/值对的开头。
I feel like there's a PHP function to help with this but I'm not finding much. I'm using Codeigniter if there's anything I can use that is provided by CI.
我觉得有一个 PHP 函数可以帮助解决这个问题,但我没有找到太多。如果我可以使用 CI 提供的任何东西,我正在使用 Codeigniter。
回答by Baba
All you need is http_build_query:
您只需要http_build_query:
$final = $url . "?" . http_build_query($subids);
回答by Yakir Sitbon
You can use with http_build_query()function.
Example from php.net:
您可以使用 withhttp_build_query()功能。来自 php.net 的示例:
<?php
$data = array(
'foo' => 'bar',
'baz' => 'boom',
'cow' => 'milk',
'php' => 'hypertext processor',
);
echo http_build_query( $data ) . "\n";
echo http_build_query( $data, '', '&' );
?>
And output this lines:
并输出这一行:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
You can read from the source: http://www.php.net/manual/en/function.http-build-query.php
您可以从来源阅读:http: //www.php.net/manual/en/function.http-build-query.php
BTW, If you use with WordPress, you can this function: http://codex.wordpress.org/Function_Reference/add_query_arg
顺便说一句,如果您使用 WordPress,您可以使用此功能:http: //codex.wordpress.org/Function_Reference/add_query_arg
Have fun. :)
玩得开心。:)
回答by Murilo
You can use http_build_query()function but be sure to do some validations if the url comes from an external function for example.
您可以使用http_build_query()函数,但如果 url 来自外部函数,请务必进行一些验证。
$url = getUrlSomewhere();
$params = ['param' => 'value', 'param2' => 'value2'];
$queryParams = http_build_query($params);
if (strpos($url, '?') !== FALSE) {
$url .= '&'. $queryParams;
} else {
$url .= '?'. $queryParams;
}
If you have PECL extension you can use http_build_url()which already takes into account if you are adding more parameters to a pre-existent URL or not among other validations it does.
如果您有 PECL 扩展,您可以使用http_build_url(),它已经考虑到您是否正在向预先存在的 URL 添加更多参数,或者在其他验证中没有。

