Using array_filter with the ARRAY_FILTER_USE_KEY flag looks like the best / fastest option.
$arrinput = array_filter( $arrinput, function($key){ return strpos($key, 'not__') !== 0; }, ARRAY_FILTER_USE_KEY );
Flag options were not added before version 5.6.0, so for older versions of PHP, a for loop would probably be the fastest option.
foreach ($arrinput as $key => $value) { if(strpos($key, 'not__') === 0) { unset($arrinput[$key]); } }
I would suggest that the following method is much slower, but this is just another way to do this.
$allowed_keys = array_filter( array_keys( $arrinput ), function($key){ return strpos($key, 'not__') !== 0; } ); $arrinput = array_intersect_key($arrinput , array_flip( $allowed_keys ));
Function
if(!function_exists('array_remove_keys_beginning_with')){ function array_remove_keys_beginning_with( $array, $str ) { if(defined('ARRAY_FILTER_USE_KEY')){ return array_filter( $array, function($key) use ($str) { return strpos($key, $str) !== 0; }, ARRAY_FILTER_USE_KEY ); } foreach ($array as $key => $value) { if(strpos($key, $str) === 0) { unset($array[ $key ]); } } return $array; } }
source share