title: Converting a querystring into an associative array
body: <p>In PHP, I need a function to convert a querystring from an URL, say: <code>http://example.com?key1=value1&amp;key2=value2</code> into a PHP associative array : <code>array ['key1' =&gt; 'value1', 'key2' =&gt; 'value2']</code>.</p>
  
  <p>I've come up to this piece of code. It works, but I find it a bit lengthy. (And PHP has built-in functions for everything: I'm surprised I haven't found anything out-of-the-box, something like the reverse of <code>http_build_query</code>.)</p>
  
  <p>Can you suggest a better way to do this?</p>
  
  <pre><code>function getUrlParams($url) {
    $querystring = parse_url($url, PHP_URL_QUERY);
    $a = explode("&amp;", $querystring);
    if (!(count($a) == 1 &amp;&amp; $a[0] == "")) {
      foreach ($a as $key =&gt; $value) {
        $b = explode("=", $value);
        $a[$b[0]] = $b[1];
        unset ($a[$key]);
      }
      return $a;
    } else {
      return false;
    }
  }
  </code></pre>