Last active
November 29, 2019 02:13
-
-
Save matthewstokeley/e313c9c56fd762e4b776d21730177bd5 to your computer and use it in GitHub Desktop.
robust find method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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