Created
June 28, 2016 08:59
-
-
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
This file contains hidden or 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 | |
/* 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