Skip to content

Instantly share code, notes, and snippets.

@jomurgel
Created June 19, 2020 16:48
Show Gist options
  • Save jomurgel/6390fd38d2b217640a274603b823f504 to your computer and use it in GitHub Desktop.
Save jomurgel/6390fd38d2b217640a274603b823f504 to your computer and use it in GitHub Desktop.
Recursively search array for value.
<?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