PHP使用数组或提取变量?

This is a question more of style than function. At the moment I use the following code to produce an array of values that have gone through various regex's in a function.

However this means having to reference the final array, which when adding lots of values to an SQL query can start to look quite messy compared to straight variables.

I could extract the array to variables after the function is called but am wondering if there is some way to do this from within the function and not run into problems with scope or should I actually avoid using straight variables and stick with the array?

<?php

function formData($form_vars)
{
    $regex_array = array(
        'alpha_num' => "/[^a-zA-Z0-9[:space:][:blank:]]/",
        'basic'     => "/[^a-zA-Z0-9[:space:][:blank:],.\\'()&-]/"
    );

    foreach ($form_vars as $key => $var) {
        $regex = $regex_array[$var];
        $val = preg_replace($regex, '',  $_REQUEST[$key]);
        $vars[$key] = $val;
    }

    return $vars;
}

$vars = array(
    'address_one'   => "basic", 
    'address_two'   => "basic", 
    'address_three' => "basic", 
    'postal'        => "alpha_num"
);

$vars = formData($vars); 

echo $vars['address_one'];

?>

To be point on your question and not diverge on other things I'd like to comment on:

$regex_array (which I'd call $softTypeFilters) and $vars (which I'd call $fieldSoftTypes) can be expternalized into a configuration file right now. So I'd stick with this and not use your other suggestion. You might loose this functionality (configurability) which is very powerful. And the code is still easy to read.

You don't need to extract $vars and can use it to build your query easier (with a function build_query($var) for example, with an other loop)

EDIT detailled answer: I think about something like this:

<?php


function formData($vars, $alias)
{
  foreach($_POST as $k => $v)
  {
    // skip unknown fields
    if (!isset($vars[$k]))
      continue;
    // deal with input names != field name
    $field_name = isset($form_alias[$k])?$form_alias[$k]:$k;

    $vars[$key] = preg_replace($regex, '',  $_REQUEST[$key]);
    if (function_exists('preprocess_'.$key))
      $vars[$key] = call_user_func('preprocess_'.$key, $vars[$key]);
  }
  return $vars;
}

function buildQuery($vars)
{
  $sql = '';
  // use a query builder, PDO prepared statements or anything 
  foreach($vars as $field => $value)
  {
    // ...
  }
  return $sql;
}

$vars = array(
  'address_one'   => "basic", 
  'address_two'   => "basic", 
  'address_three' => "basic", 
  'postal'        => "alpha_num",
);
$form_alias = array(
  'address1'   => "address_one", 
  'address2'   => "address_two", 
  'address3'   => "address_three",
);

$vars = formData($vars, $form_alias);

$query = buildQuery($vars);