Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
robust find method
<?php
/**
*
*/
function _find( $needle, $haystack, $is_key = false ) {
if ( $is_key === true ) :
return function( Callable $comparator ) use ( $needle ) {
return call_user_func( $comparator, $needle );
}
elseif ( array_key_exists( $needle, $haystack )) :
return $haystack[ $needle ];
elseif ( is_string( $needle ) &&
is_array( $haystack ) ) :
return in_array( $needle, $haystack );
elseif ( is_array( $needle ) ) &&
( count( $needle ) > 0 ) :
return array_filter( $needle, "_find" );
elseif ( preg_match( $needle, $haystack ) ) :
return preg_match( $needle, $haystack );
elseif ( is_array( $haystack ) ) :
return function( Callable $fn ) use ( $haystack ) {
return call_user_func_array( $fn, $haystack );
}
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.