function TrimArray($Input){
if (!is_array($Input))
return trim($Input);
return array_map('TrimArray', $Input);
}
/*
Old version (v0.1):
function TrimArray($arr){
if (!is_array($arr)){ return $arr; }
while (list($key, $value) = each($arr)){
if (is_array($value)){
$arr[$key] = TrimArray($value);
}
else {
$arr[$key] = trim($value);
}
}
return $arr;
}
*/
//演示范例:
$DirtyArray = array(
'Key1' => ' Value 1 ',
'Key2' => ' Value 2 ',
'Key3' => array(
' Child Array Item 1 ',
' Child Array Item 2'
)
);
$CleanArray = TrimArray($DirtyArray);
var_dump($CleanArray);
Result will be:
array(3) {
["Key1"]=>
string(7) "Value 1"
["Key2"]=>
string(7) "Value 2"
["Key3"]=>
array(2) {
[0]=>
string(18) "Child Array Item 1"
[1]=>
string(18) "Child Array Item 2"
}
}
|