PHP输入复杂的滤波阵列 [英] PHP input filtering for complex arrays

查看:109
本文介绍了PHP输入复杂的滤波阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP官方文档指出 filter_var_array()支持数组中的下列格式过滤:

  $数据=阵列(
    testarray'=>阵列('2','23','10','12')
);的$ args =阵列(
    testarray'=>阵列('过滤器'=> FILTER_VALIDATE_INT,
                            '标志'=> FILTER_FORCE_ARRAY
                           )
);$ myinputs = filter_var_array($数据,$参数);

不过,如果有问题的数组是多维的,并要求对不同地区不同的过滤器,你会怎么方法界定过滤选项?

作为一个例子:

  $数据=阵列(
    testhash'=>阵列('1级'=>电子邮件,
                           '2级'=>阵列('23','10','12'))
);


解决方案

理念1

考虑使用FILTER_CALLBACK。通过这种方式,您可以编写本身使用的过滤器扩展,从而提供一个递归能力的回调函数。

 函数validate_array(的$ args){
    返回函数($数据)使用(的$ args){
        返回filter_input_array($数据,$参数);
    };
}

这将产生回调函数。

 的$ args =阵列(
    '用户'=>阵列(
        '过滤器'=> FILTER_CALLBACK,
        '选项'=> validate_array(阵列(
            '年龄'=>阵列('过滤器'=> FILTER_INPUT_INT)
            电子邮件=>阵列('过滤器'=> FILTER_INPUT_EMAIL)
        ))
    )
);

这是什么样的配置阵列随后将会是什么样子。

思路2

不要犹豫,拍拍我的背这一个,因为我很骄傲。

以一个ARG阵列看起来像这样。斜线表示深度。

 的$ args =阵列(
    用户/年​​龄'=>阵列('过滤器'=> FILTER_INPUT_INT)
    '用户名/电子邮件'=>阵列('过滤器'=> FILTER_INPUT_EMAIL)
    用户/父母/年龄'=>阵列('过滤器'=> FILTER_INPUT_INT)
    '富'=>阵列('过滤器'=> FILTER_INPUT_INT)
);

假设你的数据看起来是这样的。

  $数据=阵列(
    '用户'=>阵列(
        '年龄'=> 15,
        电子邮件=> foo@gmail.com',
        父= GT;阵列(
            '年龄'=> 38
        )
    )
    '富'=>五
);

然后,您可以生成映射键,如用户/时代到$数据['用户'] ['年龄']引用数组。在最终的产品,你得到的东西是这样的:

 函数my_filter_array($数据的$ args){
    $ ref_map =阵列();
    的foreach($ ARGS为$关键=> $ A){
        $部分=爆炸('/',$键);
        $ REF =&放大器; $的数据;
        的foreach($部分为$ P)$ REF =安培; $文献[$ P]。
        $ ref_map [$关键] =安培; $ REF;
    }
    返回filter_var_array($ ref_map,$参数);
}后续代码var_dump(my_filter_array($数据,$参数));

现在唯一的问题是你如何处理与验证记录和原始数据集之间的不匹配。这我不能没有你知道需要如何使用它们回答。

Official PHP documentation states that filter_var_array() supports array filtering in the following format:

$data = array(
    'testarray'    => array('2', '23', '10', '12')
);

$args = array(
    'testarray'    => array('filter'    => FILTER_VALIDATE_INT,
                            'flags'     => FILTER_FORCE_ARRAY
                           )    
);

$myinputs = filter_var_array($data, $args);

However, if the array in question is multi-dimensional and requires different filters for different parts, how would you approach defining filtering options?

As an example:

$data = array(
    'testhash'    => array('level1'=>'email', 
                           'level2'=> array('23', '10', '12'))
);

解决方案

Idea 1

Consider using FILTER_CALLBACK. In this way, you can write a callback function that itself uses the filter extension, thus providing a recursive ability.

function validate_array($args) {
    return function ($data) use ($args) {
        return filter_input_array($data, $args);
    };
}

This will generate the callback functions.

$args = array(
    'user' => array(
        'filter' => FILTER_CALLBACK,
        'options' => validate_array(array(
            'age' => array('filter' => FILTER_INPUT_INT),
            'email' => array('filter' => FILTER_INPUT_EMAIL)
        ))
    )
);

This is what the config array would then look like.

Idea 2

Do not hesitate to pat me on the back for this one because I am quite proud of it.

Take an arg array that looks like this. Slashes indicate depth.

$args = array(
    'user/age' => array('filter' => FILTER_INPUT_INT),
    'user/email' => array('filter' => FILTER_INPUT_EMAIL),
    'user/parent/age' => array('filter' => FILTER_INPUT_INT),
    'foo' => array('filter' => FILTER_INPUT_INT)
);

Assume your data looks something like this.

$data = array(
    'user' => array(
        'age' => 15,
        'email' => 'foo@gmail.com',
        'parent' => array(
            'age' => 38
        )
    ),
    'foo' => 5
);

Then, you can generate an array of references that map keys such as 'user/age' to $data['user']['age']. In final production, you get something like this:

function my_filter_array($data, $args) {
    $ref_map = array();
    foreach ($args as $key => $a) {
        $parts = explode('/', $key);
        $ref =& $data;
        foreach ($parts as $p) $ref =& $ref[$p];
        $ref_map[$key] =& $ref;
    }
    return filter_var_array($ref_map, $args);
}

var_dump(my_filter_array($data, $args));

Now the only question is how you deal with the mismatch between the validation record and the original data set. This I cannot answer without knowing how you need to use them.

这篇关于PHP输入复杂的滤波阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆