VBScript array_intersect_assoc function

A VBScript equivalent of PHP’s array_intersect_assoc


Function array_intersect_assoc(mAry1,mAry2)

    Dim intersect : set intersect = Server.CreateObject("Scripting.Dictionary")
    Dim key,counter

    If isArray(mAry2) Then
        counter = uBound(mAry2)
    Else
        counter = null
    End If

    If isArray(mAry1) Then
        For key = 0 to uBound(mAry1)
            intersect.Add key, mAry1(key)
            If counter >= key or isNull(counter) Then
                If mAry2(key) <> mAry1(key) Then
                    intersect.Remove key
                End If
            End If
        Next
    ElseIf isObject(mAry1) Then
        For Each key In mAry1
            intersect.Add key, mAry1(key)
            If isNull(counter) or (isNumeric(key) and counter >= key) Then
                If mAry2(key) <> mAry1(key) Then
                    intersect.Remove key
                End If
            Else
               intersect.Remove key
            End If
        Next
    End If

    set array_intersect_assoc = intersect

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