2

Possible Duplicate:
Parse query string into an array

How can I explode a string such as:

a=1&b=2&c=3

So that it becomes:

Array {
 [a] => 1
 [b] => 2
 [c] => 3
}

Using the regular explode() function delimited on the & will separate the parameters but not in [key] => [value] pairs.

Thanks.

1

3 Answers 3

22

Use PHP's parse_str function.

$str = 'a=1&b=2&c=3';
$exploded = array();
parse_str($str, $exploded);
$exploded['a']; // 1

I wonder where you get this string from? If it's part of the URL after the question mark (the query string of an URL), you can already access it via the superglobal $_GET array:

# in script requested with http://example.com/script.php?a=1&b=2&c=3
$_GET['a']; // 1
var_dump($_GET); // array(3) { ['a'] => string(1) '1', ['b'] => string(1) '2', ['c'] => string(1) '3' )
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this perfect answer. The problem I'm getting with $_GET is that it is returning more parameters than are actually contained in the URL, so I'm getting the actual URL parameter list from $_SERVER['argv'][0] instead, but it comes in the form of a string, not an array.
@Programmer Could you elaborate? After using parse_str($_SERVER['argv'],$args), $_GET and $args should contain the exact same thing.
@Kristian, there's not much more to elaborate on, the $_GET and parse_str($_SERVER['argv'],$args) are producing different results. parse_str($_SERVER['argv'],$args) actually matches what is in the URL in the browser, while $_GET includes additional parameters not contained in the actual URL.
@Programmer: this should not happen and is very mysterious. Apart from that awkward behavior, there's no problem with additional parameters, simply don't use them in your script …
@knittl: it is strange indeed, and I think it may be caused by an API I am accessing. But I was needing the actual parameters in the URL and $_GET wasn't providing them correctly, so I needed an alternative.
|
2

Try to use the parse_str() function:

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

Comments

0

Something like this will work

$str = "a=1&b=2&c=3"
$array = array();
$elems = explode("&", $str);
foreach($elems as $elem){
    $items = explode("=", $elem);
    $array[$items[0]] = $items[1];
}

5 Comments

Re-inventing the wheel is not always the best solution … especially if the language already provides a well tested function for the problem
Additional re-inventing the wheel usually means, that you forget something. For example here you forgot urldecode()
@KingCrunch: In all fairness, the OP made no mention of this being for a URL, so the use of urldecode() could be incorrect behavior. Unlikely, but plausible. :-)
@KingCrunch. The questioner NEVER said that this was a query string. You are arbitrarily making assumptions. You are right, parse_str() would work if your assumptions are correct. Making assumptions will cost you hours in programming. What if this was just a string, and urldecode breaks the data. What if c=%20 sometimes? Think before judging
I know, I also didn't made any assumption, that it may be part of an uri, but also the OP never said, that = and & cannot be part of a key, or value, thus you must take care of it. You can replace urldecode() with every function you like, that is able to escape special characters the way its required (even if it is stupid to confuse others with "unexpected behaviour": When it looks like a query string, it should behave like one). The point is, that there are functions out there, that already can do all this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.