Parse Out Url Querystring Into an Array

This function will extract the querystring portion of any url and parse out the key/value pairs into an array with querystring key as key of the array.



	/**
	* Parse out url query string into an associative array
	*
	* $qry can be any valid url or just the query string portion.
        * Will return false if no valid querystring found
	*
	* @param $qry String
	* @return Array
	*/
	function queryToArray($qry)
	{
		$result = array();
		//string must contain at least one = and cannot be in first position
		if(strpos($qry,'=')) {
						
		 if(strpos($qry,'?')!==false) {
		   $q = parse_url($qry);
		   $qry = $q['query'];
		  }  
		}else {
			return false;
		}

		foreach (explode('&', $qry) as $couple) {
			list ($key, $val) = explode('=', $couple);
			$result[$key] = $val;
		}
        
		return empty($result) ? false : $result;

	}


If you pass in something like ‘http://wwww.domain.com/index.html?param1=val1&param2=val2′. This function will output this array.


    Array
   (
      [param1] => val1
      [param2] => val2
   )

You can also just pass in the querystring ‘param1=val1&param2=val2′ and will output the same thing.

Leave a comment

10 Comments.

  1. I like the valuable info you provide in your articles.
    I will bookmark your weblog and check again here frequently.
    I am quite sure I will learn plenty of new stuff right here!
    Best of luck for the next!

  2. This post will help the internet viewers for creating new website or even a weblog from start to
    end.

  3. I just could not go away your site before sggesting
    that I relly enjoyed the standard info an individual supply for your guests?
    Is going to be again continuously iin order to inspedt new posts

  4. I was searching this query and now i fount it

    Thanks a lot

  5. I bog frequently and I truly thank you for your content. This great article has truly
    peaked myy interest. I am going to take a note of your site and keep
    checking ffor new deetails about once peer week. I subscribed too your Feed as well.

  6. WOW just what I was looking for. Came here by searching for Nikon D5200 SLR Camera Reviews Prices

  7. Hi friends, how is all, and what you desire to say concerning this article, in my view its really awesome
    in favor of me.

  8. It all requires courage and strategy to make an ideal wedding photography project.
    This way, photographers can capture the real mood and level
    of comfort between the bride and the groom. Wedding photography is an art,
    and finding the perfect photographer is just as crucial as
    finding the perfect shoe.

  9. You’re so cool! I don’t suppose I’ve read through anything like this before.
    So wonderful to find somebody with a few unique thoughts on this subject matter.

    Really.. thank you for starting this up. This web site is something that is needed on the web,
    someone with a bit of originality!

  10. Sexting on kik is easy, fun and best of all safe.