Return to Snippet

Revision: 27911
at June 26, 2010 21:23 by sarfraznawaz2005


Initial Code
// search from array
function array_query($array,$what){
    if(in_array($what, $array)){
         return $array[array_search($what, $array)];
    }
    return false;
}


// search from recursive arrays
function recursiveArraySearch($haystack, $needle, $index = null)
{
    $aIt     = new RecursiveArrayIterator($haystack);
    $it    = new RecursiveIteratorIterator($aIt);

    while($it->valid())
    {       
        if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
            return $aIt->key();
        }

        $it->next();
    }

    return false;
}

Initial URL

                                

Initial Description
Functions to search from arrays as well as recursive arrays.

Initial Title
Array Search Function

Initial Tags
php, array

Initial Language
PHP