Skip to content

Instantly share code, notes, and snippets.

@iftee
Created June 28, 2016 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iftee/3dca21f5ed814221ef17130ff89141c7 to your computer and use it in GitHub Desktop.
Save iftee/3dca21f5ed814221ef17130ff89141c7 to your computer and use it in GitHub Desktop.
Since in_array() doesn't work for multidimensional arrays, this recursive in_array_r() does the trick
<?php
/* Check if an item $search_item is in the multidimensional array $items */
function in_array_r( $search_item, $items, $strict = false ) {
foreach ( $items as $item ) {
if ( ( $strict ? $item === $search_item : $item == $search_item ) || ( is_array( $item ) && in_array_r( $search_item, $item, $strict ) ) ) {
return true;
}
}
return false;
}
/* Usage exmaple */
// $cars = array( array( "Volvo", 22 ), array( "BMW", 15 ), array( "Saab", 5 ), array( "Land Rover", 17) );
// to check if BMW is in $cars:
// echo in_array_r( "BMW", $cars ) ? 'found' : 'not found';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment