Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
if(!function_exists('array_blacklist_assoc')){
/**
* Returns an array containing all the entries from array1 whose keys are not present in any of the other arrays when using their values as keys.
* @param array $array1 The array to compare from
* @param array $array2 The array to compare against
* @return array $array2,... More arrays to compare against
*/
function array_blacklist_assoc(Array $array1, Array $array2) {
if(func_num_args() > 2){
$args = func_get_args();
array_shift($args);
$array2 = call_user_func_array('array_merge', $args);
}
return array_diff_key($array1, array_flip($array2));
}
}
if(!function_exists('array_blacklist')){
/**
* Returns an array containing all the entries from array1 that are not present in any of the other arrays.
*
* @param array The array to compare from
* @param array $array2 An array to compare against its values.
* @param array $array2,... More arrays to compare against
* @return array
*/
function array_blacklist(Array $array1, Array $array2) {
if(func_num_args() > 2){
$args = func_get_args();
return call_user_func_array('array_diff', $args);
} else {
return array_diff($array1, $array2);
}
}
}
if(!function_exists('array_whitelist_assoc')){
/**
* Returns an associative array containing all the entries of array1 which have keys that are present in all the arguments when using their values as keys.
*
* @param array $array The array with master keys to check.
* @param array $array2 An array to compare keys against its values.
* @return array $array2,... A variable list of arrays to compare.
*
*/
function array_whitelist_assoc(Array $array1, Array $array2) {
if(func_num_args() > 2){
$args = func_get_args();
array_shift($args);
$array2 = call_user_func_array('array_merge', $args);
}
return array_intersect_key($array1, array_flip($array2));
}
}
if(!function_exists('array_whitelist')){
/**
* Returns an array containing all of the values in array1 whose values exist in all of the parameters.
*
* @param array $array1 The array with master values to check.
* @param array $array2 An array to compare values against.
* @param array $array2,... A variable list of arrays to compare.
* @return array
*/
function array_whitelist(Array $array1, Array $array2) {
if(func_num_args() > 2){
$args = func_get_args();
array_shift($args);
$array2 = call_user_func_array('array_merge', $args);
}
return array_intersect($array1, $array2);
}
}
$numeric_array = ['one','two','three','four','a','b'];
$assoc_array = ['one' =>'val1','two'=>'val2','three'=>'val3','four'=>'val4', 'a' =>'val5','b'=>'val6'];
$whitelist1 = ['one','two','three'];
$whitelist2 = ['two','four','six','eight','ten'];
$blacklist1 = ['a','b','c','d','e'];
$blacklist2 = ['a','e','i','o','u'];
var_dump(array_whitelist($numeric_array, $whitelist1));
var_dump(array_whitelist($numeric_array, $whitelist1, $whitelist2));
var_dump(array_blacklist($numeric_array, $blacklist1));
var_dump(array_blacklist($numeric_array, $blacklist1, $blacklist2));
var_dump(array_whitelist_assoc($assoc_array, $whitelist1));
var_dump(array_whitelist_assoc($assoc_array, $whitelist1, $whitelist2));
var_dump(array_blacklist_assoc($assoc_array, $blacklist1));
var_dump(array_blacklist_assoc($assoc_array, $blacklist1, $blacklist2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment