Last active
November 11, 2020 17:24
-
-
Save gh-o-st/cf9a5e8a6cb4ef7cba6a to your computer and use it in GitHub Desktop.
Recursive replacement for in_array() function
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 | |
| # 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