Skip to content

Instantly share code, notes, and snippets.

@gh-o-st
Last active November 11, 2020 17:24
Show Gist options
  • Select an option

  • Save gh-o-st/cf9a5e8a6cb4ef7cba6a to your computer and use it in GitHub Desktop.

Select an option

Save gh-o-st/cf9a5e8a6cb4ef7cba6a to your computer and use it in GitHub Desktop.
Recursive replacement for in_array() function
<?php
# Recursive replacement for in_array() function
function array_search_recursive($needle, $haystack) {
# Validate data types
if (is_array($needle) || !is_array($haystack)) {
return null;
}
# Check if in_array() will suffice
if (in_array($needle, $haystack)) {
return true;
}
# Enter recursion...
foreach ($haystack as $element) {
if (!is_array($element)) {
return null;
}
if (array_search_recursive($needle, $element)) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment