Visual Basic 6 function "array_search"

Go back

Below you'll find the source for the Visual Basic 6 function array_search.

Attribute VB_Name = "modArraySearch"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

' Searches for an index in an array
' Same syntax as the PHP function 'array_search'
' See also:
' I only didn't copy the strict (Boolean) parameter, because it don't have a purpose in Visual Basic
' @param    String  needle      The string to search for
' @param    Array   haystack    The array to search through
' @return   Integer             The index of the string in the array
' @author   Stefan Thoolen <>
Public Function array_search(needle As String, haystack() As Variant) As Integer
    Dim i As Integer, ret As Integer
    For i = LBound(haystack) To UBound(haystack)
        If haystack(i) = needle Then ret = i
    Next i
    array_search = ret
End Function