Multiple HTTP GET parameters with the same identifier

三世轮回 提交于 2019-11-27 15:06:32

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)

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.

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
Vitor Marques

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));
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!