Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Forked from ruprict/build_params.php
Created March 20, 2012 19:23
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save wilmoore/2140151 to your computer and use it in GitHub Desktop.
Retrieve whitelisted set of parameters
<?php
function filter_params(array $params = [], array $whitelist = []) {
return array_map('trim', array_intersect_key($params, array_flip($whitelist)));
}
$params = ['is_admin' => 1, 'name' => ' Bob', 'surname' => 'Smith '];
$whitelist = ['name', 'surname'];
// these are your whitelisted parameters (please don't forget to filter/validate before persisting)
var_dump(filter_params($params, $whitelist));
<?php
function filter_params(array $params = array(), array $whitelist = array()) {
return array_map('trim', array_intersect_key($params, array_flip($whitelist)));
}
$params = array('is_admin' => 1, 'name' => ' Bob', 'surname' => 'Smith ');
$whitelist = array('name', 'surname');
// these are your whitelisted parameters (please don't forget to filter/validate before persisting)
var_dump(filter_params($params, $whitelist));
@wilmoore
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment