具有相同标识符的多个HTTP GET参数

Let's say I am getting requests such as:

http://www.example.com/index.php?id=123&version=3&id=234&version=4

Is it possible to extract these in a simple way inside my php code? I realize I could get the entire querystring with javascript using window.location.href and handle it manually but I'm looking for something more elegant. The requests can contain any number of version/id pairs but I can assume that the query is well-formed and have no obligation to handle invalid strings.

duanbogan5878
duanbogan5878 这是可能的,请看这篇文章:stackoverflow.com/questions/353379/...
9 年多之前 回复
dsdukbc60905239
dsdukbc60905239 虽然查询字符串是由我们公司的软件构建的,但客户端应用程序早已在数千台计算机上交付和安装。该软件的下一次更新可能包含对语法的更改,但由于此功能与软件的自动更新有关,因此需要使用查询字符串的当前状态。
大约 10 年之前 回复
doucan9079
doucan9079 是否可以更改查询字符串的格式?例如index.php?records[id][]=123&records[version][]=3&records[id][]=234&records[version][]=4这样你就可以通过$_GET[“records”]来获取它们“id”][0]#=123和$_GET[“records”][“version”][0]#=3
大约 10 年之前 回复

5个回答



根据 PHP手册中的这条评论,PHP的查询字符串解析器会删除重复的参数...所以我不认为PHP非常适合你想做的事情( 除了它具有与javascript相同的容量以获取原始查询字符串,您可以随意执行任何操作)</ p>
</ div>

展开原文

原文

According to this comment from the PHP manual, PHP's query string parser will drop duplicate params... so I don't think that PHP is a good fit for what you want to do (except in that it has the same capacity as javascript to get the raw query string, with which you can do whatever you want)

druybew06513
druybew06513 你可能现在...我原来的答案差不多是五年了。
5 年多之前 回复
duangang4940
duangang4940 PHP支持这一点。 多值查询字符串变量进入数组,可以从那里访问。 无需解决方法。 不是PHP的问题。
5 年多之前 回复
dongxiezhi9564
dongxiezhi9564 在这个评论中有一个解决方法函数的代码示例,谢谢。
大约 10 年之前 回复
duanpao9781
duanpao9781 +1这在PHP方面看起来非常愚蠢,我认为任何其他平台都支持多值http参数而没有问题。
大约 10 年之前 回复

Assuming you have some control over the request, suffix the name with [] and PHP will generate arrays instead of dropping all but one.

http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4

Since they are pairs you'll probably want to fix the order they appear in using indexes.

http://www.example.com/index.php?id[0]=123&version[0]=3&id[1]=234&version[1]=4

If you can change the field name to include [], then PHP will create an array containing all of the matching values:

http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4

If you don't have the ability to change the field names, then as you say, you'll have to parse the querystring yourself.

dongyu1979
dongyu1979 这里有文档:php.net/manual/en/faq.html.php#faq.html.arrays在$ _POST文档的最高投票用户贡献说明中也讨论了这个功能:php.net/manual/ EN / reserved.variables.post.php
4 年多之前 回复
douchuntang2827
douchuntang2827 任何人都可以指向php.net/manual/en/index.php中的某个位置,其中记录了该功能吗?
4 年多之前 回复
dsk710351
dsk710351 第一个id {}是否应该更改为id []?
接近 8 年之前 回复

Just extract the keys and values of $_GET, use the function as:

print_array('$_GET...',$_GET);

... and the function code will be:

function print_array($title, $arr) {
    echo '<table width="100%" style="padding:10;">';
    echo '<tr><td width="30%" style="text-align:right; background-color:bisque;">key of </td><td style="background-color:bisque;">'.$title.'</td></tr>';
    foreach($arr as $key => $value) {
        echo '<tr>';
            echo '<td style="text-align:right; color:grey;">';
                echo $key;
            echo '</td>';
            echo '<td>';
                echo $value;
            echo '</td>';
        echo '</tr>';
    }
    echo '</table>';
}

Not as rounded or reliable as methods mentioned above but I use this to remove the need to [] in urls without worrying about rewriting.

$aQuery = explode("&", $_SERVER['QUERY_STRING']);
$aQueryOutput = array();
foreach ($aQuery as $param) {
    if(!empty($param)){
        $aTemp = explode('=', $param, 2);
        if(isset($aTemp[1]) && $aTemp[1] !== ""){
            list($name, $value) = explode('=', $param, 2);
            $aQueryOutput[ strtolower(urldecode($name)) ][] = urldecode(preg_replace('/[^a-z 0-9\'+-]/i', "", $value));
        }
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐