1
\$\begingroup\$

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 ?

asked Feb 10, 2017 at 14:13
\$\endgroup\$
1
  • \$\begingroup\$ More context perhaps as this really doesn't seem like a code review to me. Is this a construct you are planning to use in filter_input_array() or similar? \$\endgroup\$ Commented Feb 10, 2017 at 17:03

1 Answer 1

1
\$\begingroup\$

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);
answered Feb 10, 2017 at 15:08
\$\endgroup\$
1
  • 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\$ Commented Feb 10, 2017 at 15:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.