VBScript in_array function

A VBScript equivalent of PHP’s in_array


Function in_array(needle, haystack,strict)

    in_array = False

    If Not IsArray(needle) Then
        If Len( needle ) = 0 Then Exit Function
    End If

    If VarType(strict) <> 11 Then strict = false

    Dim key

    If isArray(needle) Then
        For Each key In needle
            in_array = in_array(key,haystack,strict)
            If in_array = true Then Exit For
        Next
        Exit Function
    End If

    If isObject( haystack ) Then
        For Each key In haystack
            If isArray(haystack( key )) or isObject(haystack( key )) Then
                in_array = in_array(needle,haystack( key ),strict)
            ElseIf ( strict and vartype(haystack( key )) = vartype(needle) and haystack( key ) = needle ) or _
               ( Not strict and haystack( key ) = needle ) Then
                    in_array = true
            End If
            If in_array = true Then Exit For
        Next
    ElseIf isArray( haystack ) Then
        For key = 0 to uBound( haystack )
            If isArray(haystack( key )) or isObject(haystack( key )) Then
                in_array = in_array(needle,haystack( key ),strict)
            ElseIf ( strict and vartype(haystack( key )) = vartype(needle) and haystack( key ) = needle ) or _
               ( Not strict and haystack( key ) = needle ) Then
                    in_array = true
            End If
            If in_array = true Then Exit For
        Next
    End If

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the array extension