You cannot do this (it won't even parse):
Code: Select all
current($DataString) = trim(current($DataString)
If you want to assign a value there has to be a dollar var on the left hand side of the expression, eg:
If you want to loop through array values, changing these values as you go, there are a variety of options.
Code: Select all
foreach($array as $key=>$value)
{
$array[$key] = doSomethingTo($value);
}
// which is pretty much the same as:
foreach(array_keys($array) as $key)
{
$array[$key] = doSomethingTo($array[$key]);
}
There is an important diiference between the two loops: in the first example, array values are copied (php4). In the second, you are looping through array keys rather than a copy array - $array[$key] refers to the original array value. This is important if the array is a list of objects. Normally you do not want to copy the object, ie:
Code: Select all
$array[] =& new Foo;
$array[] =& new Bar;
foreach(array_keys($array) as $key)
{
$array[$key]->execute();
}
(Incidentally, that's known as the
Command pattern: Foo and Bar provide the same interface to the calling script - in this case the execute() method.)
There are other ways to loop:
Code: Select all
$count = count($array);
for($i=0; $i<$count; $i++)
{
$array[$i] = doSomethingTo($array[$i]);
}
Of course this only works if $array keys are numerical, sequential, and 0-indexed ie 0, 1, 2,3, ..etc. Hence, foreach loops are more generally applicable - you don't have to assume anything about $array keys.
Finally, here's a magic quotes removal function from
WACT. This uses yet another method of looping through an array (see php manual for notes on the behaviour of the list() language construct - it might not always be what you expect). Note that, since GPC superglobals can be multi-dimensoional arrays, you need to have an is_array() check:
Code: Select all
/**
* Function for stripping magic quotes. Modifies the reference.
* @param array to remove quotes from e.g. $_GET, $_POST
* @return void
* @access public
*/
function UndoMagicSlashing(&$var) {
if(is_array($var)) {
while(list($key, $val) = each($var)) {
UndoMagicSlashing($var[$key]);
}
} else {
$var = stripslashes($var);
}
}
/**
* Strip quotes if magic quotes are on
*/
if (get_magic_quotes_gpc()) {
UndoMagicSlashing($_GET);
UndoMagicSlashing($_POST);
UndoMagicSlashing($_COOKIES);
UndoMagicSlashing($_REQUEST);
}