PHP使用数组或提取变量?

时间:2022-09-13 13:21:42

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.

但是,这意味着必须引用最终数组,当向SQL查询添加大量值时,与直接变量相比可能会开始变得非常混乱。

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'];

?>

2 个解决方案

#1


0  

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.

$ regex_array(我称之为$ softTypeFilters)和$ vars(我称之为$ fieldSoftTypes)现在可以外部化为配置文件。所以我坚持这一点,而不是使用你的其他建议。您可能会失去这个非常强大的功能(可配置性)。代码仍然易于阅读。

#2


0  

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)

您不需要提取$ vars并且可以使用它来更轻松地构建查询(例如,使用函数build_query($ var),使用其他循环)

EDIT detailled answer: I think about something like this:

EDIT详细回答:我想到这样的事情:

<?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);

#1


0  

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.

$ regex_array(我称之为$ softTypeFilters)和$ vars(我称之为$ fieldSoftTypes)现在可以外部化为配置文件。所以我坚持这一点,而不是使用你的其他建议。您可能会失去这个非常强大的功能(可配置性)。代码仍然易于阅读。

#2


0  

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)

您不需要提取$ vars并且可以使用它来更轻松地构建查询(例如,使用函数build_query($ var),使用其他循环)

EDIT detailled answer: I think about something like this:

EDIT详细回答:我想到这样的事情:

<?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);