Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

indexOf an array element

Finds the first instance of an element ($needle) in an array ($haystack). It works in ascending order (from element 0 to the array's length), can be easily modified to work in reverse if required. Will return either the index of the first occurance of false if hasn't been found.

function indexOf($needle, $haystack) {                // conversion of JavaScripts most awesome
        for ($i=0;$i<count($haystack);$i++) {         // indexOf function.  Searches an array for
                if ($haystack[$i] == $needle) {       // a value and returns the index of the *first*
                        return $i;                    // occurance
                }
        }
        return false;
}

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS