Created
June 19, 2020 16:48
Recursively search array for value.
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 | |
/** | |
* Recursively check multidimentional array for value | |
* | |
* @param string|int $needle The searched value. | |
* @param array $haystack The array. | |
* @param boolean $strict If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack. | |
* | |
* @return boolean haystack has needle. | |
*/ | |
function in_array_r( $needle, $haystack, $strict = false ) { | |
foreach ( $haystack as $item ) { | |
if ( | |
( $strict ? $item === $needle : $item == $needle ) | |
|| ( | |
is_array( $item ) | |
&& in_array_r($needle, $item, $strict ) | |
) | |
) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment