Hello, I have a function I use to remove all the whitespace from the
beginning and end of all the $_REQUEST variables. A $_REQUEST variable may
also be an array so I call it recursively. I have used as a formal
parameter a referenced passed variable. So any changes to the array inside
the function should also occur to the array outside the function.
The function I use is shown below...
//the array pointer must be reset to the start before this function is
called
// for the first time. It would be a safe idea to call
resset($inputVa riable) after last use as well.
function removeWhitespac eRecursively(&$ anArray){
while (list($key,$val ) = each ($anArray)){
if (is_array($val) ){
removeWhitespac eRecursively($v al);
$anArray[$key] = $val; //unknown why required.
}else{
$anArray[$key] = trim($val);
}
}
}
In testing this function. It worked well for simple $_REQUEST variables
that had no arrays in them but when an array was in the $_REQUEST array then
the line marked "//unknown why required" was found to be needed otherwise
the changes to the recursively altered array did not propagate to the
original array.
eg...
given the input...
$_REQUEST['name'] = " noel ";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";
The output without the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";
The output with the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = "1234";
$_REQUEST['phone'][1] = "5678";
Can anyone please explain why the changes to the recursively changed array
did not propagate to the original array?
Thanks for your help.
beginning and end of all the $_REQUEST variables. A $_REQUEST variable may
also be an array so I call it recursively. I have used as a formal
parameter a referenced passed variable. So any changes to the array inside
the function should also occur to the array outside the function.
The function I use is shown below...
//the array pointer must be reset to the start before this function is
called
// for the first time. It would be a safe idea to call
resset($inputVa riable) after last use as well.
function removeWhitespac eRecursively(&$ anArray){
while (list($key,$val ) = each ($anArray)){
if (is_array($val) ){
removeWhitespac eRecursively($v al);
$anArray[$key] = $val; //unknown why required.
}else{
$anArray[$key] = trim($val);
}
}
}
In testing this function. It worked well for simple $_REQUEST variables
that had no arrays in them but when an array was in the $_REQUEST array then
the line marked "//unknown why required" was found to be needed otherwise
the changes to the recursively altered array did not propagate to the
original array.
eg...
given the input...
$_REQUEST['name'] = " noel ";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";
The output without the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = " 1234 ";
$_REQUEST['phone'][1] = " 5678 ";
The output with the marked line was...
$_REQUEST['name'] = "noel";
$_REQUEST['phone'][0] = "1234";
$_REQUEST['phone'][1] = "5678";
Can anyone please explain why the changes to the recursively changed array
did not propagate to the original array?
Thanks for your help.
Comment