Adam Gotterer

Find the secrets to infinite income, and automate it!

Archive for July, 2008

Recursive PHP in_array function

I needed a recursive in_array function the other day and disliked all the samples I found on php.net. I wrote this one using the StandardPHPLibrary. It will recursively search through a multidimensional array and return true if the $needle is found in the $haystack.

 
function in_array_recursive($needle, $haystack) {
 
    $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack));
 
    foreach($it AS $element) {
        if($element == $needle) {
            return true;
        }
    }
 
    return false;
}
  • 1 Comment
  • Filed under: PHP
  • Personal Life

    I moved my personal life over to tumblr.adamgotterer.com. Feel free to follow my life by the minute. This site will now only be related to tech posts and my ventures.

  • 0 Comments
  • Filed under: General