public
Last active — forked from ruprict/build_params.php

Retrieve whitelisted set of parameters

  • Download Gist
with_5.4.php
PHP
1 2 3 4 5 6 7 8 9 10 11
<?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));
without_5.4.php
PHP
1 2 3 4 5 6 7 8 9 10 11
<?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));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.