note 102745 deleted from reserved.variables.request by danbrown
| From: | danbrown@php.net | Date: | Thu, 03 Mar 2011 13:58:34 +0000 |
| Subject: | note 102745 deleted from reserved.variables.request by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177530@lists.php.net to get a copy of this message | ||
Note Submitter: e dot reggi at afterbabel dot net
----
I've implemented John Galt's function as to be able to pass a multi-dimensional array as
input parameter. This lets you add, change, or even delete several $_GET's variables at a time.
<?php
function update_gets($parameter) {
$output = "?";
$firstRun = true;
for($index = 0; $index < count($parameter); $index++) {
foreach($_GET as $key=>$val) {
if($key != $parameter[$index][0]) {
if(!isset($_GET[$key]) && $parameter[$index][1] != NULL) {
if(!$firstRun) {
$output .= "&";
}
else {
$firstRun = false;
}
$output .= $key."=".urlencode($val);
$_GET[$key] = $val;
}
}
else {
if($parameter[$index][1] != NULL) {
if(!$firstRun) {
$output .= "&";
}
else {
$firstRun = false;
}
$output .= $key."=".urlencode($parameter[$index][1]);
$_GET[$key] = $parameter[$index][1];
}
else {
unset($_GET[$key]);
}
}
}
if(!isset($_GET[$parameter[$index][0]]) && $parameter[$index][1] != NULL) {
if(!$firstRun) {
$output .= "&";
}
else {
$firstRun = false;
}
$output .= $parameter[$index][0]."=".urlencode($parameter[$index][1]);
$_GET[$parameter[$index][0]] = $parameter[$index][1];
}
}
return htmlentities($output);
}
?>
__________________________
#########################################
#########################################
## ##
## Structure of $parameter array ##
## ##
#########################################
#########################################
<?php
$parameter = array {
[0] => array {
[0] => $key;
[1] => $value;
}
...
[n] => array {
[0] => $key;
[1] => $value;
}
}
?>
#################################################################
#################################################################
## ##
## Example Usage ##
## ##
## WARNING: THIS FUNCTION WORKS EXACTELY LIKE JOHN ##
## GALT'S ONE, WHICH MEANS YOU CAN USE IT ##
## THE SAME WAY, JUST PASSING AN ARRAY, ##
## STRUCTURED AS SHOWN ABOVE, AS INPUT ##
## ARGUMENT. ##
## ##
#################################################################
#################################################################
<?php
#Deleting an existent $_GET's variable, which name is $key:
$parameter[0][0] = $key;
$parameter[0][1] = NULL;
update_gets($parameter);
#Adding or Changing an existent $_GET's variable, which name is $key:
$parameter[0][0] = $key;
$parameter[0][1] = $new_value;
update_gets($parameter);
?>