15

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.

3
  • Is it possible to change the query string's format? such as index.php?records[id][]=123&records[version][]=3&records[id][]=234&records[version][]=4 so that you could get them by $_GET["records"]["id"][0] #= 123 and $_GET["records"]["version"][0] #= 3
    – PeterWong
    Oct 20, 2010 at 16:48
  • While the querystring is constructed by our company's software, the client application has been long since delivered and installed on thousands of computers. The next update of that software can contain changes to the syntax, but since this functionality is related to the automatic updates of the software, it needs to work with the current state of the querystrings.
    – MatsT
    Oct 20, 2010 at 16:51
  • This is possible, see this post: stackoverflow.com/questions/353379/… May 4, 2011 at 19:42

5 Answers 5

27

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.

3
8

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)

4
  • 15
    +1 This seems pretty stupid on the PHP side, I think any other platform supports multivalued http parameters without problems.
    – leonbloy
    Oct 20, 2010 at 16:56
  • There's a code example for a workaround function in that very comment, thank you.
    – MatsT
    Oct 20, 2010 at 16:57
  • 2
    PHP does support this. Multivalued querystring variables go into an array and can be accessed from there. No workaround needed. Not a PHP problem.
    – mmundiff
    Apr 11, 2015 at 17:58
  • 2
    @mmundiff You may be right now... my original answer is almost five years old. Apr 12, 2015 at 19:25
1

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
1

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>';
}
1

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));
        }
    }
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.