VBScript array_udiff function

A VBScript equivalent of PHP’s array_udiff



Function array_udiff(mAry1,mAry2,data_compare_func)

    Dim arr_udif,key_c,key,found

    If Not isObject(mAry1) or Not isObject(mAry2) Then
        set array_diff_assoc = retAry : Exit Function
    End If

    If isArray(mAry1) and isArray(mAry2) Then

        For Each key In mAry1

            found = 0
            For Each key_c In mAry2
                execute("found = " & data_compare_func & "(key, key_c)")
                If found <> 0 Then
                    Exit For
                End If
            Next

            If found > 0 Then
                [] arr_udif, mAry1(key)
            ElseIf found < 0 Then
                [] arr_udif, mAry2(key_c)
            End If
        Next

        array_udiff = arr_udif

    ElseIf isObject(mAry1) and isObject(mAry2) Then

        set arr_udif = Server.CreateObject("Scripting.Dictionary")

        For Each key In mAry1

            found = 0
            For Each key_c In mAry2
                execute("found = " & data_compare_func & "(mAry1(key), mAry2(key_c))")
                If found <> 0 Then
                    Exit For
                End If
            Next

            If found > 0 Then
                If arr_udif.Exists(key) Then
                    arr_udif.Item(key) = mAry1(key)
                Else
                    arr_udif.Add key, mAry1(key)
                End If
            ElseIf found < 0 Then
                If arr_udif.Exists(key_c) Then
                    arr_udif.Item(key_c) = mAry2(key_c)
                Else
                    arr_udif.Add key_c, mAry2(key_c)
                End If
            End If

        Next

        set array_udiff = arr_udif

    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