\$\begingroup\$
\$\endgroup\$
1
I have two arrays as follows:
public $headers = [
'user_id',
'timestamp',
'value',
];
protected $args = array('user_id' => array('filter' => FILTER_VALIDATE_REGEXP,
'options' => array('regexp' => '/^[a-zA-Z0-9_]*$/'), ),
'timestamp' => array('filter' => FILTER_CALLBACK,
'options' => "validateDate", ), ),
'value' => FILTER_VALIDATE_FLOAT, );
The keys in the array args are the same values of the array headers. How to avoid repeating the values here ?
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
Here is the answer:
I delete the keys from the args array, and I create a new array from combining both headers and args:
$argsVal = [ array('filter' => FILTER_VALIDATE_REGEXP,'options' => array('regexp' => '/^[a-zA-Z0-9_:.()\/]*$/'), ),
array('filter' => FILTER_CALLBACK,'options' => "validateDate", ), FILTER_VALIDATE_FLOAT, ];
$args = array_combine($headers,$argsVal);
-
1\$\begingroup\$ Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and how it improves upon the original) so that the author can learn from your thought process. \$\endgroup\$Vogel612– Vogel6122017年02月10日 15:47:27 +00:00Commented Feb 10, 2017 at 15:47
lang-php
filter_input_array()
or similar? \$\endgroup\$