querystring remove duplicate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

querystring remove duplicate

Post by louie35 »

This is doing my **** in.

Does anybody knows a clean solution for removing duplicated string + values from a querystring?

ex. page.php?1=1&2=2&1=3 to page.php?2=2&1=3
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

parse_str() combined with something else, maybe?

PHP will automatically handle the smashing down of the information if you're worried about that.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

tried that, didn't work as it was supposed to or I was missing something.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I believe apache will only process the last instance of a variable in the query string..

as feyd mentioned, if your physically manipulating the string then parse_str(), and perhaps a funky combination of array_unique(), and array_keys() should work.
User avatar
louie35
Forum Contributor
Posts: 144
Joined: Fri Jan 26, 2007 8:40 am
Location: Dublin
Contact:

Post by louie35 »

all i needed was a clear head. I came up with this:

Code: Select all

$a = explode("&", $_SERVER['QUERY_STRING']);
  $query = array_unique($a);
   foreach($query as $q_name){
    $query_s .= $q_name."&";
  }
  $query_s = rtrim($query_s,"&");//remove last trailing &
if you know a better solution let me know.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

This can be written shorter with implode().
Post Reply