VBScript array_reduce function

A VBScript equivalent of PHP’s array_reduce


Function array_reduce(ByVal mAry, callback, ByVal initial)

    array_reduce = null
    If len( initial ) > 0 Then array_reduce = initial
    If not isArray( mAry ) and not isObject( mAry ) Then Exit Function

    Dim acc : acc = initial
    Dim key

    If isObject( mAry ) Then
        For Each key In mAry
            execute("acc = " & callback & "(acc, mAry(key))")
        Next

    ElseIf isArray( mAry ) Then

        Dim lon : lon = uBound( mAry )
        For key = 0 to lon
            execute("acc = " & callback & "(acc, mAry(key))")
        Next
    End If

    array_reduce = acc

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