VBScript array_intersect_uassoc function

A VBScript equivalent of PHP’s array_intersect_uassoc


Function array_intersect_uassoc(mAry1,mAry2,key_compare_func)

    Dim result : set result = Server.CreateObject("Scripting.Dictionary")
    Dim key,k,found,compare

    If isArray(mAry1) Then
        For key = 0 to uBound(mAry1)
            found = false

            If isArray(mAry2) Then
                For k = 0 to uBound(mAry2)
                    execute("compare = " & key_compare_func & "(key,k)")
                    If compare = 0 and mAry1(key) = mAry2(k) Then
                        found = true
                        Exit For
                    End If
                Next
            ElseIf isObject(mAry2) Then
                For Each k In mAry2
                    execute("compare = " & key_compare_func & "(key,k)")
                    If compare = 0 and mAry1(key) = mAry2(k) Then
                        found = true
                        Exit For
                    End If
                Next
            End If

            If found = true Then
                result.Add k, mAry1(key)
            End if
        Next
    ElseIf isObject(mAry1) Then
        For Each key In mAry1
            found = false

            If isArray(mAry2) Then
                For k = 0 to uBound(mAry2)
                    execute("compare = " & key_compare_func & "(key,k)")
                    If compare = 0 and mAry1(key) = mAry2(k) Then
                        found = true
                        Exit For
                    End If
                Next
            ElseIf isObject(mAry2) Then
                For Each k In mAry2
                    execute("compare = " & key_compare_func & "(key,k)")
                    If compare = 0 and mAry1(key) = mAry2(k) Then
                        found = true
                        Exit For
                    End If
                Next
            End If

            If found = true Then
                result.Add k, mAry1(key)
            End if
        Next
    End If

    set array_intersect_uassoc = result

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