note 45493 added to function.parse-str

From: Date: Thu, 09 Sep 2004 03:46:38 +0000
Subject: note 45493 added to function.parse-str
Groups: php.notes 
Request: Send a blank email to php-notes+get-76269@lists.php.net to get a copy of this message
If the querystring contains duplicate keys in the key-value pairs, parse_str will only return the last instance of the value. For example, in the following: <?php $mystr = "test1=blah&test2=bleh&test1=burp"; parse_str($mystr, $myarray); echo $myarray['test1']; ?> The value output will be 'burp'. I wrote a function that takes a querystring and returns the the key-value pairs as a two-dimensional array so each duplicate key is available: <?php $str = "test1=blah&test2=bleh&test1=burp"; $valsarray = parse_str_ext($str); echo $valsarray['test1'][0]; echo $valsarray['test1'][1]; echo $valsarray['test2'][0]; function parse_str_ext($toparse) { $returnarray = array(); $keyvaluepairs = split("&", $toparse); foreach($keyvaluepairs as $pairval) { $splitpair = split("=", $pairval); if(!array_key_exists($splitpair[0], $returnarray)) $returnarray[$splitpair[0]] = array(); $returnarray[$splitpair[0]][] = $splitpair[1]; } return $returnarray; } ?> Output will be: blah burp bleh ---- Manual Page -- http://www.php.net/manual/en/function.parse-str.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45493 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45493&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45493&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#76269) next »