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.

Community
  • 1
  • 1
ProgrammerGirl
  • 3,157
  • 7
  • 45
  • 82
  • And http://stackoverflow.com/questions/8643938/php-fastest-method-to-parse-url-params-into-variables. There are tons of these questions. – kba Jan 28 '12 at 15:22

3 Answers3

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' )
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 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. – ProgrammerGirl Jan 28 '12 at 15:19
  • @Programmer Could you elaborate? After using `parse_str($_SERVER['argv'],$args)`, `$_GET` and `$args` should contain the exact same thing. – kba Jan 28 '12 at 15:24
  • @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. – ProgrammerGirl Jan 28 '12 at 15:28
  • 1
    @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 Jan 28 '12 at 15:33
  • @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. – ProgrammerGirl Jan 28 '12 at 15:36
  • That API is broken then. – knittl Jan 28 '12 at 15:37
  • I think It can appears when you use RewriteRule on .htaccess – dievardump Jan 28 '12 at 15:39
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
T30
  • 11,422
  • 7
  • 53
  • 57
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
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];
}
James L.
  • 4,032
  • 1
  • 15
  • 15
  • Re-inventing the wheel is not always the best solution … especially if the language already provides a well tested function for the problem – knittl Jan 28 '12 at 15:16
  • 1
    Additional re-inventing the wheel usually means, that you forget something. For example here you forgot `urldecode()` – KingCrunch Jan 28 '12 at 15:18
  • 1
    @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. :-) – FtDRbwLXw6 Jan 28 '12 at 15:21
  • 1
    @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 – James L. Jan 28 '12 at 15:30
  • 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. – KingCrunch Jan 28 '12 at 15:35