phpstringquery-stringtext-parsingurl-parsing

Parse a querystring into an associative array


Looking for suggestions as to the best way to do this (I know there are many options):

take a variable like this:

$args = 'post_type=post&order_by=DESC&limit=10';

the create a function to convert that variable into parts like this:

$post_type = post;
$order_by = DESC;
$limit= 10;

Reason is I want a client to be able to use a string like the first to pass variables to a sql query in much the same way (but doesn't have to be exact) as you can do it in WordPress.


Solution

  • You might want to take a look at

    http_build_query()

    and

    parse_str() ( in your case it's more parse_str() but you'll need the first one for "doing the opposite" )