[英]How to get multiple values of same param for url
I'm building a PHP querying a mongodo database.我正在构建一个查询 mongodo 数据库的 PHP。 I need to retreive the value of a param from the url, and there is potentially more than one.
我需要从 url 中检索参数的值,并且可能不止一个。
An example url would look like this一个示例 url 看起来像这样
http://localhost/api/v1/report-01?type=EE&type=ER http://localhost/api/v1/report-01?type=EE&type=ER
How would I retrieve the two values from type
.我将如何从
type
检索这两个值。
At the moment I'm only get one, and it's the last one.目前我只有一个,而且是最后一个。
if (isset($params["TYPE"]) && in_array($params["TYPE"], ["EE", "ER"])) {
$matchPipeline["TYPE"] = $params["TYPE"];
echo "Printing Variables";
echo $params["TYPE"];
}
The code is only printing ER
该代码仅打印
ER
This SO answer provides a good suggestion on how to do this.这个 SO answer为如何做到这一点提供了一个很好的建议。 Add [] to the parameter name, so in your case the url might look like:
将 [] 添加到参数名称,因此在您的情况下 url 可能如下所示:
http://localhost/api/v1/report-01?type[]=EE&type[]=ER http://localhost/api/v1/report-01?type[]=EE&type[]=ER
Then PHP will automatically create an array of all the type[] values.然后 PHP 将自动创建一个包含所有 type[] 值的数组。
Just add to your link after every param(type) this []只需在每个参数(类型)这个 [] 之后添加到您的链接
example http://localhost/api/v1/report-01?type[]=EE&type[]=ER例如 http://localhost/api/v1/report-01?type[]=EE&type[]=ER
or follow this或按照这个
<?php
$params = $_GET;
/*
echo $params['type'][0];
-> ANSWER = EE;
echo $params['type'][1];
-> ANSWER = ER;
*/
if(isset($params['type']) && is_array($params)){
echo "Printing Variables<br>";
foreach ($params as $get){
echo $get[0]."<br>";
echo $get[1]."<br>";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.