0

I am using the answer here https://stackoverflow.com/a/27689796/3842368 to strip off several parameters from a url. I thought it was working, until today when I found out that unset() wasn't working if I defined multiple parameters.

My PHP code is:

$url = $_SERVER["REQUEST_URI"];
$x = $url;
$parsed = parse_url($x);
$query = $parsed['query'];
parse_str($query, $params);
unset($params['qdef, qval1']);
$string = http_build_query($params);

$params is producing the following array, using print_r:

Array ( [use_url] => on [zipcode] => Axminster, Devon EX13 5RZ [categories] => Array ( [0] => 2 ) [radius] => 0 [checkboxnational] => 1 [submit] => loading results... [yesq] => y [qdef] => 2 [qval1] => 100.00 [qval2] => 525.00 [seasoning] => 1 [slider-value1] => 0 [slider-value2] => 70 [resultbar] => 25 [sortbar] => distance [lat] => 50.77643440000001 [lng] => -2.9810417 [swlat] => 50.7773526197085 [swlng] => -2.9814507999999478 [nelat] => 50.7800505802915 [nelng] => -2.9749947000000247 [outcode] => EX13 ) 1

so the problem I have is that if I write unset($params['qdef']); then qdef is stripped from the url, but if I write unset($params['qdef, qval1']); then nothing is stripped. I've tried separating them with a comma, enclosing each parameter in '[]', nothing works. I've tried many ways of defining the parameters in the unset() command but nothing works.

Any thoughts?

Community
  • 1
  • 1
luke_mclachlan
  • 1,035
  • 1
  • 15
  • 35

1 Answers1

1

$params['qdef'] and $params['qval1'] are separate elements in the array

use

unset($params['qdef'], $params['qval1']);

to unset them individually

unset($params['qdef, qval1']); 

is trying to unset a single element from $params with a key of 'qdef, qval1'

Mark Baker
  • 209,507
  • 32
  • 346
  • 385