To apply the same filter to many params/keys, use array_fill_keys().
<?php
$data = array(
'product_id' => 'libgd<script>',
'component' => ' 10 ',
'versions' => '2.0.33',
'testscalar' => array('2', '23', '10', '12'),
'testarray' => '2',
);
$keys = array(
'product_id',
'component',
'versions',
'doesnotexist',
'testscalar',
'testarray'
);
$options = array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
);
$args = array_fill_keys($keys, $options);
/* Result
$args = array(
'product_id' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'component' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'versions' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'doesnotexist' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'testscalar' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
'testarray' => array(
'filter' => FILTER_CALLBACK,
'options' => function ($value) {
return trim(strip_tags($value));
},
),
);
*/
$myinputs = filter_var_array($data, $args);
var_dump($myinputs);
Output:
array(6) {
'product_id' =>
string(5) "libgd"
'component' =>
string(2) "10"
'versions' =>
string(6) "2.0.33"
'doesnotexist' =>
NULL
'testscalar' =>
array(4) {
[0] =>
string(1) "2"
[1] =>
string(2) "23"
[2] =>
string(2) "10"
[3] =>
string(2) "12"
}
'testarray' =>
string(1) "2"
}