Search Associative Array with Wildcard in PHP

Here are a pair of functions that will allow you to search an associative array for either a key or a value, using a string with a wildcard (*).

function array_key_exists_wildcard ( $array, $search, $return = '' ) {
    $search = str_replace( '\*', '.*?', preg_quote( $search, '/' ) );
    $result = preg_grep( '/^' . $search . '$/i', array_keys( $array ) );
    if ( $return == 'key-value' )
    	return array_intersect_key( $array, array_flip( $result ) );
    return $result;
}

function array_value_exists_wildcard ( $array, $search, $return = '' ) {
    $search = str_replace( '\*', '.*?', preg_quote( $search, '/' ) );
    $result = preg_grep( '/^' . $search . '$/i', array_values( $array ) );
    if ( $return == 'key-value' )
    	return array_intersect( $array, $result );
    return $result;
}

$array = array(
	'test_123'   => 'sbr123',
	'Test_12345' => 'bbb456',
	'test_222'   => 'bry789',
	'test_ewrwe' => 'abc777',
	't1est_eee'  => 'def950'
);

$search = 'test*';
print_r( array_key_exists_wildcard( $array, $search ) );
print_r( array_key_exists_wildcard( $array, $search, 'key-value' ) );

$search = 'b*';
print_r( array_value_exists_wildcard( $array, $search ) );
print_r( array_value_exists_wildcard( $array, $search, 'key-value' ) );

/*
Outputs:
Array
(
    [0] => test_123
    [1] => Test_12345
    [2] => test_222
    [3] => test_ewrwe
)
Array
(
    [test_123] => sbr123
    [Test_12345] => bbb456
    [test_222] => bry789
    [test_ewrwe] => abc777
)
Array
(
    [1] => bbb456
    [2] => bry789
)
Array
(
    [Test_12345] => bbb456
    [test_222] => bry789
)
*/

Kudo’s to logic_earth

3 thoughts on “Search Associative Array with Wildcard in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s