VBScript array_diff_uassoc function

A VBScript equivalent of PHP’s array_diff_uassoc


Function array_diff_uassoc(ByVal mAry1,ByVal mAry2,key_compare_func)

    Dim arr_dif
    set arr_dif = Server.CreateObject("Scripting.Dictionary")

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

    Dim j,k,callback_ret
    For Each j in mAry1

        arr_dif.Add j, mAry1(j)

        For Each k In mAry2
            If mAry1(j) = mAry2(k) Then
                execute("callback_ret = " & key_compare_func & "(j,k)")
                If callback_ret = 0 Then
                    If arr_dif.Exists(j) Then arr_dif.Remove j
                ElseIf callback_ret < 0 Then
                    arr_dif.Remove j
                    If arr_dif.Exists(k) Then
                        arr_dif.Item( k ) = mAry2(k)
                    Else
                        arr_dif.Add k ,mAry2(k)
                    End If
                End If
            End If
        Next
    Next

    set array_diff_uassoc = arr_dif

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