Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active November 29, 2019 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/e313c9c56fd762e4b776d21730177bd5 to your computer and use it in GitHub Desktop.
Save matthewstokeley/e313c9c56fd762e4b776d21730177bd5 to your computer and use it in GitHub Desktop.
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